Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 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
Next Next commit
Fix false positive in Final local scope variable
  • Loading branch information
GiorgosPapoutsakis committed Jun 3, 2024
commit 11a802773eae6a67a69aae9b81f2ddd050be3dc6
3 changes: 2 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(at least as I'm reading it, this is the key change and hence my question about line 1623)

self.msg.protocol_members_cant_be_final(s)
if (
isinstance(s.rvalue, TempNode)
and s.rvalue.no_rhs
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-final.test
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,37 @@ class P(Protocol):
pass
[out]

[case testFinalInProtocol]
from typing import Protocol, Final, final, ClassVar

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

[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

Expand Down