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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3137,7 +3137,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 @@ -3316,6 +3316,14 @@ 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()
or inferred.name == "__all__"
and self.scope.is_top_level()
):
str_type = self.named_type("builtins.str")
return self.named_generic_type("typing.Iterable", [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
23 changes: 22 additions & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,28 @@ 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
[builtins fixtures/module_all.pyi]
[out]
main:2: error: Type of __all__ must be "Sequence[str]", not "int"

[case testModuleAllEmptyList]
__all__ = []
[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
10 changes: 10 additions & 0 deletions test-data/unit/check-slots.test
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,16 @@ class A:
self.missing = 3
[builtins fixtures/dict.pyi]

[case testSlotsNotInClass]
# Shouldn't be triggered
__slots__ = [1, 2]

def foo() -> None:
__slots__ = 1

[case testSlotsEmptyList]
class A:
__slots__ = []

[case testSlotsWithAny]
from typing import Any
Expand Down