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

Prevent crash when enum/typeddict call is stored as a class attribute#18861

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
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
NextNext commit
Prevent crash when enum call is stored into class attribute
  • Loading branch information
@sterliakov
sterliakov committedMar 31, 2025
commiteec2cab457e8b8e357263e77af7e7b23650853d8
4 changes: 2 additions & 2 deletionsmypy/semanal.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3464,8 +3464,8 @@ def record_special_form_lvalue(self, s: AssignmentStmt) -> None:
def analyze_enum_assign(self, s: AssignmentStmt) -> bool:
"""Check if s defines an Enum."""
if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, EnumCallExpr):
# Already analyzed enum --nothing to do here.
returnTrue
# Already analyzed enum --only recheck that it can be stored correctly.
returnlen(s.lvalues) == 1 and isinstance(s.lvalues[0], NameExpr)
return self.enum_call_analyzer.process_enum_call(s, self.is_func_scope())

def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
Expand Down
12 changes: 12 additions & 0 deletionstest-data/unit/check-enum.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2512,3 +2512,15 @@ def list_vals(e: Type[T]) -> list[T]:

reveal_type(list_vals(Choices)) # N: Revealed type is "builtins.list[__main__.Choices]"
[builtins fixtures/enum.pyi]

[case testEnumAsClassMemberNoCrash]
# https://github.com/python/mypy/issues/18736
from enum import Enum

class Base:
def __init__(self, namespace: tuple[str, ...]) -> None:
# Not a bug: trigger defer
names = [name for name in namespace if fail] # E: Name "fail" is not defined
self.o = Enum("o", names) # E: Enum type as attribute is not supported \
# E: Second argument of Enum() must be string, tuple, list or dict literal for mypy to determine Enum members
[builtins fixtures/tuple.pyi]

[8]ページ先頭

©2009-2025 Movatter.jp