Skip to content
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
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3286,7 +3286,7 @@ def analyze_identity_global_assignment(self, s: AssignmentStmt) -> bool:

Return true if special casing was applied.
"""
if not isinstance(s.rvalue, NameExpr) or len(s.lvalues) != 1:
if not isinstance(s.rvalue, NameExpr) or len(s.lvalues) != 1 or s.type is not None:
# Not of form 'X = X'
return False
lvalue = s.lvalues[0]
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-final.test
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,19 @@ class MyClass:
a: None
a: Final[int] = 1 # E: Cannot redefine an existing name as final # E: Name "a" already defined on line 5

[case testFinalCannotBeRedefinition]
from typing import Final

# This time travel is suboptimal but good enough for now?
a = 0 # E: Cannot assign to final name "a"
b = 0 # E: Cannot assign to final name "b"

a: Final = a # E: Cannot redefine an existing name as final
b: Final = 1 # E: Cannot redefine an existing name as final

a = 1 # E: Cannot assign to final name "a"
b = 1 # E: Cannot assign to final name "b"

[case testFinalOverrideAllowedForPrivate]
from typing import Final, final

Expand Down
Loading