Skip to content

Model instance attributes have correct types when accessed, but not when compared structurally to another class #134

Closed
@ckarnell

Description

@ckarnell

Currently when you access an attribute on a model, its type gets converted to the expected Python type. However, since this uses the plugin hook for attribute access, that means when structural typing is attempted, it doesn't match up.

Here's an example illustrating my point.

from typing_extensions import Protocol
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer

class UserStructure(Protocol):
    id: int

Base = declarative_base()

class User(Base):
    id = Column(Integer, primary_key=True)

user: UserStructure = User() # Incompatible types in assignment (expression has type "User", variable has type "UserStructure")  [assignment]
# Following member(s) of "User" have conflicts:
#    id: expected "int", got "Column[int]"

Even though reveal_type(user.id) shows int.

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