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

Give self argument the correct type when using check_untyped_defs #7530

Merged
merged 4 commits into from Sep 19, 2019

Conversation

@msullivan
Copy link
Collaborator

msullivan commented Sep 18, 2019

This is accomplished by updating the helper that constructs a callable type
from a func def to fill in the self argument.

This will make check_untyped_defs a lot more useful.

@msullivan msullivan requested review from JukkaL and ilevkivskyi Sep 18, 2019
@msullivan msullivan force-pushed the check-untyped-self branch from 43c2b66 to b1c3238 Sep 18, 2019
Copy link
Collaborator

ilevkivskyi left a comment

Thanks! Looks very good. Could you please add Fixes #... to the PR description for relevant issues? I believe these are:
#7309
#5401
#4637
#1514

@classmethod
def bar(cls):
cls.baz() # E: "Type[Foo]" has no attribute "baz"
[builtins fixtures/classmethod.pyi]

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Sep 19, 2019

Collaborator

I would add also tests for two other reported problems:

  • An attribute initialized with 0 in untyped __init__ has revealed type int in another untyped method
  • Descriptors also work as expected in untyped methods.

This comment has been minimized.

Copy link
@JukkaL

JukkaL Sep 19, 2019

Collaborator

Also, if an attribute is initialized to None in __init__ the inferred type should probably be either Optional[Any] or Any (the first one may generate many false positives). If an attribute is initialized to [], the inferred type should probably be List[Any], and I think that we shouldn't require a type annotation.

mypy/typeops.py Outdated Show resolved Hide resolved
mypy/typeops.py Show resolved Hide resolved
@msullivan msullivan merged commit 855076b into master Sep 19, 2019
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@msullivan msullivan deleted the check-untyped-self branch Sep 19, 2019
@The-Compiler

This comment has been minimized.

Copy link
Contributor

The-Compiler commented Oct 22, 2019

As a data point: In my project I just introduced check_untyped_defs a week ago - after the upgrade to 0.740 I now see 469 new errors 😆

Thanks for this, though! I have some work ahead of me now, but it indeed seems to make mypy a lot more useful for codebases which aren't fully annotated yet.

facebook-github-bot added a commit to pytorch/pytorch that referenced this pull request Oct 27, 2019
…27445)

Summary:
`__iter__` methods are supposed to return iterators (https://docs.python.org/3/reference/datamodel.html#object.__iter__), but some of them are typed to return iterables, which is too general. This results in error messages such as `Iterable[Module[Any]]" has no attribute "__next__"` from Mypy. Technically this should also have caused a type error [here](https://github.com/pytorch/pytorch/blob/8f7020bbdbb5537bf1954cd252523cb17ab879b1/torch/nn/modules/container.py#L115), but due to a bug in Mypy type checking isn't working correctly in untyped methods (this will be fixed in the next release though: python/mypy#7530).
Pull Request resolved: #27445

Reviewed By: lerks

Differential Revision: D18113966

Pulled By: fmassa

fbshipit-source-id: c6261ac866f86df4328e6d2fdfca0625aa2d2492
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.