Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Show file tree
Hide file tree
Changes fromall 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 deletionmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsmypy/checker_shared.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletiontest-data/unit/check-modules.test
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest-data/unit/check-slots.test
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp