Skip to content

Fix crash on redefined class variable annotated with Final[<type>] #12951

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2022
Merged
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
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