Repository navigation
Give self argument the correct type when using check_untyped_defs - #7530
Merged
Merged
Conversation
msullivan
force-pushed
the
check-untyped-self
branch
from
September 18, 2019 23:02
43c2b66 to
b1c3238
Compare
ilevkivskyi
approved these changes
Sep 19, 2019
| @classmethod | ||
| def bar(cls): | ||
| cls.baz() # E: "Type[Foo]" has no attribute "baz" | ||
| [builtins fixtures/classmethod.pyi] |
Member
There was a problem hiding this comment.
I would add also tests for two other reported problems:
- An attribute initialized with
0in untyped__init__has revealed typeintin another untyped method - Descriptors also work as expected in untyped methods.
Collaborator
There was a problem hiding this comment.
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.
Contributor
|
As a data point: In my project I just introduced 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
pushed 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
laurentdupin
pushed a commit
to laurentdupin/pytorch
that referenced
this pull request
Apr 24, 2026
…ytorch#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/bd49d877c803dc2bc0fe6e11bef482f1805a3aeb/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: pytorch#27445 Reviewed By: lerks Differential Revision: D18113966 Pulled By: fmassa fbshipit-source-id: c6261ac866f86df4328e6d2fdfca0625aa2d2492
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.