Skip to content

Doesn't find column on child class #96

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

Open
bochecha opened this issue Aug 1, 2019 · 4 comments
Open

Doesn't find column on child class #96

bochecha opened this issue Aug 1, 2019 · 4 comments
Labels

Comments

@bochecha
Copy link

bochecha commented Aug 1, 2019

Here is a very simplified reproducer:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import Column
from sqlalchemy.types import Integer


Base = declarative_base()


class BaseMixin(Base):
    __abstract__ = True

    field1 = Column(Integer)

    @classmethod
    def from_something(cls, something_complicated: int) -> 'BaseMixin':
        # Real code tries to figure out which model to use
        return MyModel(field1=something_complicated, field2=something_complicated)


class MyModel(BaseMixin):
    __tablename__ = 'first-model'

    field2 = Column(Integer)

With sqlalchemy-stubs from master, I get the following:

$ pipenv run mypy model.py 
model.py:17: error: Unexpected column "field2" for model "MyModel"

But field2 exists on the model. 😕

This seems to be due to the order of the code because if I add the following line at the end of the file, then Mypy doesn't report any error:

m = MyModel(field1=1, field2=2)
@bochecha
Copy link
Author

bochecha commented Aug 1, 2019

The problem seems to be due to

if isinstance(sym.node, Var) and isinstance(sym.node.type, Instance):

When Python passes over that line for BaseMixin.field1, both conditions are True.

But when it passes over it for MyModel.field2, the second condition is False because sym.node.type is None.

As a result, field2 doesn't get added to expected_types, which leads to the error.

I'm not sure how/where sym.node.type is supposed to be computed, maybe this is a bug in mypy instead?

@ilevkivskyi
Copy link
Contributor

ilevkivskyi commented Aug 7, 2019

I think your diagnosis is correct, but this may be not easy to fix with the current state of mypy internals (we can't trigger a deferral from this point, because module top-levels can't be deferred).

Anyway, this is a known limitation in mypy, and it will likely be removed at some point soon.

@bochecha
Copy link
Author

bochecha commented Sep 3, 2019

Anyway, this is a known limitation in mypy

Do you happen to have a link to a mypy issue handy?

@ilevkivskyi
Copy link
Contributor

Here is the link python/mypy#6356

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

No branches or pull requests

2 participants