Skip to content

Scalar SQLAlchemy association proxies are inferred as lists #8043

Backlog

Description

@harsh21234i

Describe the bug

Reflex infers every SQLAlchemy association_proxy as list[...], including scalar proxies. Accessing a scalar proxy through a state Var therefore gives it the wrong Var type and makes operations supported by its actual Python type fail at compile time.

To Reproduce

import reflex as rx
from sqlalchemy import ForeignKey
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column, relationship


class Base(MappedAsDataclass, DeclarativeBase):
    pass


class Profile(Base):
    __tablename__ = "profile"
    id: Mapped[int] = mapped_column(init=False, primary_key=True)
    name: Mapped[str]


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(init=False, primary_key=True)
    profile_id: Mapped[int] = mapped_column(ForeignKey("profile.id"), init=False)
    profile: Mapped[Profile] = relationship()
    profile_name = association_proxy("profile", "name")


class State(rx.State):
    user: User | None = None


# Raises VarAttributeError during app construction:
# State.user.profile_name is inferred as list[str], which has no .upper().
name = State.user.profile_name.upper()

The proxy is scalar (User.profile_name.scalar is True) and User(profile=Profile(name="Ada")).profile_name evaluates to a str. But get_attribute_access_type(User, "profile_name") returns list[str], and the State Var reports VarAttributeError: ... type list[str] has no attribute 'upper'.

Expected behavior

Infer the proxied attribute type for scalar association proxies, while retaining list[...] inference for collection proxies. Then string, numeric, and object operations should match the Python value exposed by the proxy.

Specifics

  • Verified on current upstream main (6f5c39cac).
  • The inference is in packages/reflex-base/src/reflex_base/utils/types.py, in the AssociationProxyInstance branch of get_attribute_access_type.

Additional context

PR #2809 added association-proxy support, but its inference wraps every proxy in List[...]; that behavior is correct for collection proxies but not scalar proxies. The separate #4147 report concerns pickling an association-proxy creator lambda, and the recent DeclarativeBase serializer PRs concern runtime serialization, not this compile-time type inference failure.

Activity

  1. linear-code commented on Nov 28, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions