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

Add type stubs for the sqlalchemy models #69

Merged
merged 2 commits into from
May 19, 2018
Merged

Conversation

PeterJCLaw
Copy link
Contributor

This means that we get real type checking for the model types, rather than the Any checking which we were previously getting.

This currently errors due to the issue which #68 fixes (though amazingly shows no other issues), so I consider this blocked until that merges.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 845be46 on models-type-stubs into ca9d805 on master.

# sqlalchemy actually allows the attribute to be used for ordering like
# this; ignore the type check here specifically rather than complicate
# our type definitions.
History.id.desc(), # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels non-ideal. Is there a way to declare the types of the fields on the type and instances separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that doesn't seem to be possible at the moment:

from typing import ClassVar


class Thing:
    class_attr: ClassVar[int] = 0

    def __init__(self):
        self.instance_attr: str = ""
        self.class_attr: str = ''


reveal_type(Thing.class_attr)
reveal_type(Thing.instance_attr)  # I'd hope this would error as not existing

t = Thing()
reveal_type(t.class_attr)
reveal_type(t.instance_attr)
$ mypy thing.py 
thing.py:12: error: Revealed type is 'builtins.int'
thing.py:13: error: Revealed type is 'builtins.str'
thing.py:16: error: Revealed type is 'builtins.int'
thing.py:17: error: Revealed type is 'builtins.str'

python/mypy#4886 seems to suggest there might be a solution at some point, however I don't think mypy is there yet.

@danpalmer danpalmer merged commit 0989713 into master May 19, 2018
@danpalmer
Copy link
Contributor

@PeterJCLaw hope you don't mind, I've merged this. Happy to review any further changes in another PR.

@danpalmer danpalmer deleted the models-type-stubs branch May 19, 2018 09:13
@PeterJCLaw
Copy link
Contributor Author

@danpalmer no worries; I'd actually forgotten about this PR. (I think I wasn't sure whether your comment was blocking, hence not merging it at the time)

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

Successfully merging this pull request may close these issues.

3 participants