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 from1 commit
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
PrevPrevious commit
NextNext commit
Apply same treatment to __all__
  • Loading branch information
@sterliakov
sterliakov committedJun 27, 2025
commit84b8849ee3d7ae6e9c0c09c0a5e244415d24a78d
7 changes: 6 additions & 1 deletionmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3316,7 +3316,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():
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
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
23 changes: 22 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,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

[8]ページ先頭

©2009-2025 Movatter.jp