You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 <[email protected]>
Co-authored-by: Stanislav Terliakov
Copy file name to clipboardExpand all lines: test-data/unit/check-final.test
+44Lines changed: 44 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -301,6 +301,50 @@ class P(Protocol):
301
301
pass
302
302
[out]
303
303
304
+
[case testFinalInProtocol]
305
+
from typing import Final, Protocol, final
306
+
307
+
class P(Protocol):
308
+
var1 : Final[int] = 0 # E: Protocol member cannot be final
309
+
310
+
@final # E: Protocol member cannot be final
311
+
def meth1(self) -> None:
312
+
var2: Final = 0
313
+
314
+
def meth2(self) -> None:
315
+
var3: Final = 0
316
+
317
+
def meth3(self) -> None:
318
+
class Inner:
319
+
var3: Final = 0 # OK
320
+
321
+
@final
322
+
def inner(self) -> None: ...
323
+
324
+
class Inner:
325
+
var3: Final = 0 # OK
326
+
327
+
@final
328
+
def inner(self) -> None: ...
329
+
330
+
[out]
331
+
332
+
[case testFinalWithClassVarInProtocol]
333
+
from typing import Protocol, Final, final, ClassVar
334
+
335
+
class P(Protocol):
336
+
var1 : Final[ClassVar[int]] = 0 # E: Variable should not be annotated with both ClassVar and Final
337
+
var2: ClassVar[int] = 1
338
+
339
+
@final # E: Protocol member cannot be final
340
+
def meth1(self) -> None:
341
+
...
342
+
343
+
def meth2(self) -> None:
344
+
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
0 commit comments