-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Infer empty list without annotation for __slots__
and module __all__
#19348
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
base: master
Are you sure you want to change the base?
Infer empty list without annotation for __slots__
and module __all__
#19348
Conversation
__slots__
__slots__
__slots__
__slots__
and module __all__
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
reveal_type(__slots__) # N: Revealed type is "builtins.int" | ||
|
||
[case testSlotsEmptyList] | ||
class A: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to add more types to the test? Like set
, dict
, tuple
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will work with set, won't work with dict (because there's second type argument), will work with tuple (for unrelated reasons, though, ()
has a well-defined type tuple[()]
, context won't come into play here). I don't want to pin this dict
behavior in test as it isn't ideal, we already have cases with empty tuple. Pushed a testcase with set
to have Iterable
vs Sequence
difference captured by test, that's a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't work with dict (because there's second type argument
But, __slots__
can be in a dict
form:
>>> class A:
... __slots__ = {'a': 'docs'}
...
>>> A().a = 1
>>> A().b = 2
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
A().b = 2
^^^^^
AttributeError: 'A' object has no attribute 'b' and no __dict__ for setting new attributes
It can also be empty:
>>> class A:
... __slots__ = {}
Shouldn't we support this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's fine at runtime, we have tests for that, only empty dict will still produce "need type annotation".
Shouldn't we support this case?
IMO not. First of all, what's the type of __slots__
there? It's dict[str, ???]
, we do not have enough information to fill in the second argument. It's no worse than asking for type annotations on empty dicts elsewhere, is it? I don't think this use case is popular enough to add more special-casing down the road. However, I don't think it is important to raise "need type annotation" there, so do not want to add a test explicitly for this behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can leave this for later :)
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Fixes #10870, fixes #10103.
This adds a fake
Iterable[str]
context when checking the following:__all__ = []
at top level__slots__ = []
at class level (also works for sets but not for dicts)Additionally, this fixes a bug with
__slots__
being mistakenly checked in other contexts (at top level or in function bodies), so e.g. the following is now accepted: