From ad0e180659f8bb1ef0c270045c655888b3b1223a Mon Sep 17 00:00:00 2001 From: GiorgosPapoutsakis <116210016+GiorgosPapoutsakis@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:38:17 +0300 Subject: [PATCH] Fix false positive for Final local scope variable in Protocol (#17308) This PR fixes and closes #17281 ,which reported a false positive when using Final within the local function scope of a protocol method. With these changes: - Local variables within protocol methods can be marked as Final. - Protocol members still cannot be marked as Final Modified ``semanal.py`` file and added a unit test in ``test-data/unit/check.final.test`` --------- Co-authored-by: Jelle Zijlstra Co-authored-by: Stanislav Terliakov --- mypy/semanal.py | 3 ++- test-data/unit/check-final.test | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index e4f123cbfc20..2448ea8485f7 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -3505,7 +3505,8 @@ def unwrap_final(self, s: AssignmentStmt) -> bool: if self.loop_depth[-1] > 0: self.fail("Cannot use Final inside a loop", s) if self.type and self.type.is_protocol: - self.msg.protocol_members_cant_be_final(s) + if self.is_class_scope(): + self.msg.protocol_members_cant_be_final(s) if ( isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs diff --git a/test-data/unit/check-final.test b/test-data/unit/check-final.test index 26a0d0782503..dadf76a283b0 100644 --- a/test-data/unit/check-final.test +++ b/test-data/unit/check-final.test @@ -301,6 +301,50 @@ class P(Protocol): pass [out] +[case testFinalInProtocol] +from typing import Final, Protocol, final + +class P(Protocol): + var1 : Final[int] = 0 # E: Protocol member cannot be final + + @final # E: Protocol member cannot be final + def meth1(self) -> None: + var2: Final = 0 + + def meth2(self) -> None: + var3: Final = 0 + + def meth3(self) -> None: + class Inner: + var3: Final = 0 # OK + + @final + def inner(self) -> None: ... + + class Inner: + var3: Final = 0 # OK + + @final + def inner(self) -> None: ... + +[out] + +[case testFinalWithClassVarInProtocol] +from typing import Protocol, Final, final, ClassVar + +class P(Protocol): + var1 : Final[ClassVar[int]] = 0 # E: Variable should not be annotated with both ClassVar and Final + var2: ClassVar[int] = 1 + + @final # E: Protocol member cannot be final + def meth1(self) -> None: + ... + + def meth2(self) -> None: + var3: Final[ClassVar[int]] = 0 # E: Variable should not be annotated with both ClassVar and Final # E: ClassVar can only be used for assignments in class body + +[out] + [case testFinalNotInLoops] from typing import Final