Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
6bf8602cdc257384b88493139f49e083e5c585bc153117300File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3138,7 +3138,7 @@ def check_assignment( | ||
| else: | ||
| self.check_getattr_method(signature, lvalue, name) | ||
| if name == "__slots__" and self.scope.active_class() is not None: | ||
| typ = lvalue_type or self.expr_checker.accept(rvalue) | ||
| self.check_slots_definition(typ, lvalue) | ||
| if name == "__match_args__" and inferred is not None: | ||
| @@ -3317,6 +3317,12 @@ def get_variable_type_context(self, inferred: Var, rvalue: Expression) -> Type | | ||
| type_contexts.append(base_type) | ||
| # Use most derived supertype as type context if available. | ||
| if not type_contexts: | ||
| if inferred.name == "__slots__" and self.scope.active_class() is not None: | ||
| str_type = self.named_type("builtins.str") | ||
| return self.named_generic_type("typing.Iterable", [str_type]) | ||
sterliakov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if inferred.name == "__all__" and self.scope.is_top_level(): | ||
| str_type = self.named_type("builtins.str") | ||
| return self.named_generic_type("typing.Sequence", [str_type]) | ||
| return None | ||
| candidate = type_contexts[0] | ||
| for other in type_contexts: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -496,6 +496,29 @@ class A: | ||
| self.missing = 3 | ||
| [builtins fixtures/dict.pyi] | ||
| [case testSlotsNotInClass] | ||
| # Shouldn't be triggered | ||
| __slots__ = [1, 2] | ||
sterliakov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| reveal_type(__slots__) # N: Revealed type is "builtins.list[builtins.int]" | ||
| def foo() -> None: | ||
| __slots__ = 1 | ||
sterliakov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| reveal_type(__slots__) # N: Revealed type is "builtins.int" | ||
| [case testSlotsEmptyList] | ||
| class A: | ||
sterliakov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| __slots__ = [] | ||
| reveal_type(__slots__) # N: Revealed type is "builtins.list[builtins.str]" | ||
| reveal_type(A.__slots__) # N: Revealed type is "builtins.list[builtins.str]" | ||
| [case testSlotsEmptySet] | ||
| class A: | ||
| __slots__ = set() | ||
| reveal_type(__slots__) # N: Revealed type is "builtins.set[builtins.str]" | ||
| reveal_type(A.__slots__) # N: Revealed type is "builtins.set[builtins.str]" | ||
| [builtins fixtures/set.pyi] | ||
| [case testSlotsWithAny] | ||
| from typing import Any | ||