Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ckarnell opened this issue Dec 18, 2019 · 2 comments

Comments

@ckarnell
Copy link
Contributor

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.

@ilevkivskyi
Copy link
Contributor

This is actually caused by mypy issue python/mypy#7724. But I will keep this open as an important example.

@ilevkivskyi
Copy link
Contributor

Actually there is a dedicated issue already for this case python/mypy#5481, so this can be closed as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants