Skip to content

Incorrect type error for Mapped[...] SQLAlchemy 2.0 attributes used in instance methods #445

@ll01

Description

@ll01

Describe the Bug

When using SQLAlchemy 2.0-style ORM models with Mapped[...] = mapped_column(...), Pyrefly incorrectly infers that class-level attributes (e.g., ContactRecord.organization) are being passed to functions as values, even inside instance methods.

Expected behavior:
When referencing self.organization, Pyrefly should recognize this as the instance value (str), not the InstrumentedAttribute[str].

Actual behavior:
Pyrefly emits an error such as:
Argument InstrumentedAttribute[str]is not assignable to parameterorganizationwith typestr``
…even when self.organization is correctly used within an instance method.

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBAJghgFzgYwDZwM4YKYagSwgAcwQFZEV0sAoUSKBATyPwDsBzA408geSIJ8YNnFR1w0DAEd0qZAAtsEJtxJkoASTYJsHbCAA0UAMoIQ7DhIYy5i5UwB0paIXXkAItjRwQifABu2ABCmNjGALJwRETYMMYQ0bEwAPrIYKgArhBsNDQ+WFChOAAUXj5+QkHF2ACUAFw0UM1QKSns+AhtUAC8UAByIth5NAAC8EgFGPnUeADCIpMIjS0EMPUEOk0tynD4qBsCQiJiANoY5gC6282kHHBs+ABe-iIbFyB5U1ALOigIACVvKQYCUag0bq0UkgAEaobCiCDYbp9ABE6T+yAQGFReVW+HWUCiMTip3YCEuvSgiRJqXSWRyJW0un0RigRAsiRATBSAGtsEwegAVECZcJQOCZBBgdjIEDKBEIYWiuqQ3b7DbE5KnI7CUSoc5XSl9GnJNIZbJsEpmCycEoAJgArI7asY2JlUOg4dhlWLapC7g9nq82JqkqSPsbqeG6RbGTbLCUAIwABhTrqg7s9cG9PQAYmIcP7ITBsMBGGAUjBIHsrThUMBalAALQAPh+i3+K1WLXlCEyIDYHcxCBKkJ7LQJPXrwEcBMM44nUHVqGn2AbjhXC6XPcDjxexzYa43e+Dh+MUAAxFAADzN5tQAAKTHlwFQqjfcA4eAQCnweFQdhsE2dIQHlLF30XZpi1WUYpiRX8wBgEsyygegICrGt2BKNAMGMDElg2X4libNsoFRYj-iBUCYFRbsJz7Ach1wscd3xGAegI-453iKCexXTjOyxTdEn2bc2NuEB7n3ENBJHZwpKDA89XEidaiAA

(Only applicable for extension issues) IDE Information

Version: 0.18.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions