Skip to content

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3138,7 +3138,7 @@ def check_assignment(
else:
self.check_getattr_method(signature, lvalue, name)

if name == "__slots__":
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:
Expand Down Expand Up @@ -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])
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:
Expand Down
4 changes: 4 additions & 0 deletions mypy/checker_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ def current_self_type(self) -> Instance | TupleType | None:
return fill_typevars(item)
return None

def is_top_level(self) -> bool:
"""Is current scope top-level (no classes or functions)?"""
return len(self.stack) == 1

@contextmanager
def push_function(self, item: FuncItem) -> Iterator[None]:
self.stack.append(item)
Expand Down
30 changes: 29 additions & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,35 @@ import typing
__all__ = [1, 2, 3]
[builtins fixtures/module_all.pyi]
[out]
main:2: error: Type of __all__ must be "Sequence[str]", not "list[int]"
main:2: error: List item 0 has incompatible type "int"; expected "str"
main:2: error: List item 1 has incompatible type "int"; expected "str"
main:2: error: List item 2 has incompatible type "int"; expected "str"

[case testAllMustBeSequenceStr2]
import typing
__all__ = 1 # E: Type of __all__ must be "Sequence[str]", not "int"
reveal_type(__all__) # N: Revealed type is "builtins.int"
[builtins fixtures/module_all.pyi]

[case testAllMustBeSequenceStr3]
import typing
__all__ = set() # E: Need type annotation for "__all__" (hint: "__all__: set[<type>] = ...") \
# E: Type of __all__ must be "Sequence[str]", not "set[Any]"
reveal_type(__all__) # N: Revealed type is "builtins.set[Any]"
[builtins fixtures/set.pyi]

[case testModuleAllEmptyList]
__all__ = []
reveal_type(__all__) # N: Revealed type is "builtins.list[builtins.str]"
[builtins fixtures/module_all.pyi]

[case testDunderAllNotGlobal]
class A:
__all__ = 1

def foo() -> None:
__all__ = 1
[builtins fixtures/module_all.pyi]

[case testUnderscoreExportedValuesInImportAll]
import typing
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-slots.test
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,29 @@ class A:
self.missing = 3
[builtins fixtures/dict.pyi]

[case testSlotsNotInClass]
# Shouldn't be triggered
__slots__ = [1, 2]
reveal_type(__slots__) # N: Revealed type is "builtins.list[builtins.int]"

def foo() -> None:
__slots__ = 1
reveal_type(__slots__) # N: Revealed type is "builtins.int"

[case testSlotsEmptyList]
class A:
__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
Expand Down