Skip to content

Commit

Permalink
Fix crash on redefined class variable annotated with Final[<type>] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Jun 7, 2022
1 parent 7e38877 commit c3e32e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 8 additions & 7 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2715,13 +2715,14 @@ def check_final(self,
if is_final_decl and self.scope.active_class():
lv = lvs[0]
assert isinstance(lv, RefExpr)
assert isinstance(lv.node, Var)
if (lv.node.final_unset_in_class and not lv.node.final_set_in_init and
not self.is_stub and # It is OK to skip initializer in stub files.
# Avoid extra error messages, if there is no type in Final[...],
# then we already reported the error about missing r.h.s.
isinstance(s, AssignmentStmt) and s.type is not None):
self.msg.final_without_value(s)
if lv.node is not None:
assert isinstance(lv.node, Var)
if (lv.node.final_unset_in_class and not lv.node.final_set_in_init and
not self.is_stub and # It is OK to skip initializer in stub files.
# Avoid extra error messages, if there is no type in Final[...],
# then we already reported the error about missing r.h.s.
isinstance(s, AssignmentStmt) and s.type is not None):
self.msg.final_without_value(s)
for lv in lvs:
if isinstance(lv, RefExpr) and isinstance(lv.node, Var):
name = lv.node.name
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-final.test
Original file line number Diff line number Diff line change
Expand Up @@ -1109,3 +1109,11 @@ class A(ABC):
@final # E: Method B is both abstract and final
@abstractmethod
def B(self) -> None: ...

[case testFinalClassVariableRedefinitionDoesNotCrash]
# This used to crash -- see #12950
from typing import Final

class MyClass:
a: None
a: Final[int] = 1 # E: Cannot redefine an existing name as final # E: Name "a" already defined on line 5

0 comments on commit c3e32e3

Please sign in to comment.