Debian Codegraph @

test-data/unit/check-final.test

raw
1 -- Test cases for final qualifier
2 --
3
4 -- Definitions
5
6 [case testFinalDefiningModuleVar]
7 from typing import Final
8
9 x: Final = int()
10 y: Final[float] = int()
11 z: Final[int] = int()
12 bad: Final[str] = int() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
13
14 reveal_type(x) # N: Revealed type is "builtins.int"
15 reveal_type(y) # N: Revealed type is "builtins.float"
16 reveal_type(z) # N: Revealed type is "builtins.int"
17 [out]
18
19 [case testFinalDefiningInstanceVar]
20 from typing import Final
21
22 class C:
23 x: Final = int()
24 y: Final[float] = int()
25 z: Final[int] = int()
26 bad: Final[str] = int() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
27 class D(C): pass
28
29 reveal_type(D.x) # N: Revealed type is "builtins.int"
30 reveal_type(D.y) # N: Revealed type is "builtins.float"
31 reveal_type(D.z) # N: Revealed type is "builtins.int"
32 reveal_type(D().x) # N: Revealed type is "builtins.int"
33 reveal_type(D().y) # N: Revealed type is "builtins.float"
34 reveal_type(D().z) # N: Revealed type is "builtins.int"
35 [out]
36
37 [case testFinalDefiningInstanceVarImplicit]
38 from typing import Final, Tuple, Any
39
40 class C:
41 def __init__(self, x: Tuple[int, Any]) -> None:
42 self.x: Final = x
43 self.y: Final[float] = 1
44 reveal_type(C((1, 2)).x) # N: Revealed type is "tuple[builtins.int, Any]"
45 reveal_type(C((1, 2)).y) # N: Revealed type is "builtins.float"
46 [builtins fixtures/tuple.pyi]
47 [out]
48
49 [case testFinalBadDefinitionTooManyArgs]
50 from typing import Final
51
52 x: Final[int, str] # E: Final name must be initialized with a value \
53 # E: Final[...] takes at most one type argument
54 reveal_type(x) # N: Revealed type is "builtins.int"
55
56 class C:
57 def __init__(self) -> None:
58 self.x: Final[float, float] = 1 # E: Final[...] takes at most one type argument
59 reveal_type(C().x) # N: Revealed type is "builtins.float"
60 [out]
61
62 [case testFinalInvalidDefinitions]
63 from typing import Final, Any
64
65 x = y = 1 # type: Final[float] # E: Invalid final declaration
66 z: Any
67 z[0]: Final[int] # E: Invalid final declaration \
68 # E: Unexpected type declaration
69 [out]
70
71 [case testFinalDefiningInstanceVarStubs]
72 # Allow skipping r.h.s.
73 import mod
74 [file mod.pyi]
75 from typing import Final
76
77 x: Final # E: Type in Final[...] can only be omitted if there is an initializer
78 y: Final[int]
79 class C:
80 x: Final # E: Type in Final[...] can only be omitted if there is an initializer
81 y: Final[int]
82 def __init__(self) -> None:
83 self.z: Final # E: Type in Final[...] can only be omitted if there is an initializer
84
85 reveal_type(x) # N: Revealed type is "Any"
86 reveal_type(C.x) # N: Revealed type is "Any"
87 v: C
88 reveal_type(v.z) # N: Revealed type is "Any"
89 [out]
90
91 [case testFinalDefiningFunc]
92 from typing import final
93
94 @final # E: @final cannot be used with non-method functions
95 def f(x: int) -> None: ...
96 [out]
97
98 [case testFinalDefiningFuncOverloaded]
99 from typing import final, overload
100
101 @overload
102 def f(x: int) -> int: ...
103 @overload
104 def f(x: str) -> str: ...
105 @final # E: @final cannot be used with non-method functions
106 def f(x):
107 pass
108 [out]
109
110 [case testFinalDefiningMeth]
111 from typing import final
112
113 class C:
114 @final
115 def f(self, x: int) -> None: ...
116 reveal_type(C().f) # N: Revealed type is "def (x: builtins.int)"
117 [out]
118
119 [case testFinalDefiningMethOverloaded]
120 from typing import final, overload
121
122 class C:
123 @overload
124 def f(self, x: int) -> int: ...
125 @overload
126 def f(self, x: str) -> str: ...
127 @final
128 def f(self, x):
129 pass
130
131 @overload
132 def bad(self, x: int) -> int: ...
133 @final # E: @final should be applied only to overload implementation
134 @overload
135 def bad(self, x: str) -> str: ...
136 def bad(self, x):
137 pass
138
139 reveal_type(C().f) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
140 [out]
141
142 [case testFinalDefiningMethOverloadedStubs]
143 from mod import C
144
145 reveal_type(C().f) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
146 [file mod.pyi]
147 from typing import final, overload
148
149 class C:
150 @final
151 @overload
152 def f(self, x: int) -> int: ...
153 @overload
154 def f(self, x: str) -> str: ...
155
156 @overload
157 def bad(self, x: int) -> int: ...
158 @final # E: In a stub file @final must be applied only to the first overload
159 @overload
160 def bad(self, x: str) -> str: ...
161
162 [case testFinalDefiningProperty]
163 from typing import final
164
165 class C:
166 @final
167 @property
168 def f(self) -> int: pass
169 @property
170 @final
171 def g(self) -> int: pass
172 reveal_type(C().f) # N: Revealed type is "builtins.int"
173 reveal_type(C().g) # N: Revealed type is "builtins.int"
174 [builtins fixtures/property.pyi]
175 [out]
176
177 [case testFinalDefiningOuterOnly]
178 from typing import Final, Callable, Tuple, Any
179 x: Tuple[Final] # E: Final can be only used as an outermost qualifier in a variable annotation
180 y: Callable[[], Tuple[Final[int]]] # E: Final can be only used as an outermost qualifier in a variable annotation
181 [builtins fixtures/tuple.pyi]
182 [out]
183
184 [case testFinalDefiningNotInMethod]
185 from typing import Final
186
187 def f(x: Final[int]) -> int: ... # E: Final can be only used as an outermost qualifier in a variable annotation
188 def g(x: int) -> Final[int]: ... # E: Final can be only used as an outermost qualifier in a variable annotation
189 [out]
190
191 [case testFinalDefiningNotInMethodExtensions]
192 # flags: --python-version 3.14
193 from typing_extensions import Final
194
195 def f(x: Final[int]) -> int: ... # E: Final can be only used as an outermost qualifier in a variable annotation
196 def g(x: int) -> Final[int]: ... # E: Final can be only used as an outermost qualifier in a variable annotation
197 [builtins fixtures/tuple.pyi]
198 [out]
199
200 [case testFinalDefiningNoRhs]
201 from typing import Final
202 x: Final # E: Type in Final[...] can only be omitted if there is an initializer
203 y: Final[int] # E: Final name must be initialized with a value
204 class C:
205 x: Final # E: Type in Final[...] can only be omitted if there is an initializer
206 y: Final[int] # E: Final name must be initialized with a value
207 def __init__(self) -> None:
208 self.z: Final # E: Type in Final[...] can only be omitted if there is an initializer
209 reveal_type(x) # N: Revealed type is "Any"
210 reveal_type(y) # N: Revealed type is "builtins.int"
211 reveal_type(C().x) # N: Revealed type is "Any"
212 reveal_type(C().y) # N: Revealed type is "builtins.int"
213 reveal_type(C().z) # N: Revealed type is "Any"
214 [out]
215
216 [case testFinalDefiningNoRhsSubclass]
217 from typing import Final
218
219 class A:
220 x: Final[int] # E: Final name must be initialized with a value
221
222 class B(A):
223 x = 1 # E: Cannot assign to final name "x"
224 def __init__(self) -> None:
225 self.x = 1 # E: Cannot assign to final attribute "x"
226 [out]
227
228 [case testFinalDefiningNoTypevarsExplicit]
229 from typing import Final, TypeVar, Generic, Tuple, Any
230
231 T = TypeVar('T')
232 d: Any
233
234 class C(Generic[T]):
235 x: Final[Tuple[T, T]] = d # E: Final name declared in class body cannot depend on type variables
236 [builtins fixtures/tuple.pyi]
237 [out]
238
239 [case testFinalDefiningTypevarsImplicit]
240 from typing import Final, TypeVar, Generic, Tuple, Any
241
242 T = TypeVar('T')
243
244 class C(Generic[T]):
245 def __init__(self, x: Tuple[T, T]) -> None:
246 self.x: Final = x
247 self.y: Final = 1
248
249 reveal_type(C((1, 2)).x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
250 C.x # E: Cannot access final instance attribute "x" on class object \
251 # E: Access to generic instance variables via class is ambiguous
252 C.y # E: Cannot access final instance attribute "y" on class object
253 [builtins fixtures/tuple.pyi]
254 [out]
255
256 [case testFinalDefiningNotInOtherMethod]
257 from typing import Final, Any, Tuple
258
259 class C:
260 def meth(self, x: Tuple[int, Any]) -> None:
261 self.x: Final = x # E: Can only declare a final attribute in class body or __init__
262 self.y: Final[float] = 1 # E: Can only declare a final attribute in class body or __init__
263 [builtins fixtures/tuple.pyi]
264 [out]
265
266 [case testFinalDefiningOnlyOnSelf]
267 from typing import Final, Any, Tuple
268
269 class U:
270 x: Any
271 y: Any
272 class C:
273 def __init__(self, x: Tuple[int, Any]) -> None:
274 slf = U()
275 slf.x: Final = x # E: Final can be only applied to a name or an attribute on self
276 slf.y: Final[float] = 1 # E: Type cannot be declared in assignment to non-self attribute \
277 # E: Final can be only applied to a name or an attribute on self
278 [builtins fixtures/tuple.pyi]
279 [out]
280
281 [case testFinalNotInProtocol]
282 from typing import Final, final, Protocol, overload
283
284 class P(Protocol):
285 x: Final[float] = 1 # E: Protocol member cannot be final
286 @final # E: Protocol member cannot be final
287 def meth(self, x) -> int:
288 pass
289 @overload
290 def other(self, x: int) -> int: ...
291 @overload
292 def other(self, x: str) -> str: ...
293 @final # E: Protocol member cannot be final
294 def other(self, x):
295 pass
296 [out]
297
298 [case testFinalInProtocol]
299 from typing import Final, Protocol, final
300
301 class P(Protocol):
302 var1 : Final[int] = 0 # E: Protocol member cannot be final
303
304 @final # E: Protocol member cannot be final
305 def meth1(self) -> None:
306 var2: Final = 0
307
308 def meth2(self) -> None:
309 var3: Final = 0
310
311 def meth3(self) -> None:
312 class Inner:
313 var3: Final = 0 # OK
314
315 @final
316 def inner(self) -> None: ...
317
318 class Inner:
319 var3: Final = 0 # OK
320
321 @final
322 def inner(self) -> None: ...
323
324 [out]
325
326 [case testFinalWithClassVarInProtocol]
327 from typing import Protocol, Final, final, ClassVar
328
329 class P(Protocol):
330 var1 : Final[ClassVar[int]] = 0 # E: Variable should not be annotated with both ClassVar and Final
331 var2: ClassVar[int] = 1
332
333 @final # E: Protocol member cannot be final
334 def meth1(self) -> None:
335 ...
336
337 def meth2(self) -> None:
338 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
339
340 [out]
341
342 [case testFinalNotInLoops]
343 from typing import Final
344
345 for i in [1, 2, 3]:
346 x: Final = i # E: Cannot use Final inside a loop
347
348 while True:
349 y: Final = True # E: Cannot use Final inside a loop
350 [builtins fixtures/list.pyi]
351 [out]
352
353 [case testFinalDelayedDefinition]
354 from typing import Final
355
356 class C:
357 x: Final[int] # OK, defined in __init__
358 bad: Final[int] # E: Final name must be initialized with a value
359
360 def __init__(self, x: int) -> None:
361 self.x = x # OK, deferred definition
362 self.x = 2 # E: Cannot assign to final attribute "x"
363
364 def meth(self) -> None:
365 self.x = 2 # E: Cannot assign to final attribute "x"
366
367 c: C
368 c.x = 3 # E: Cannot assign to final attribute "x"
369 class D(C):
370 x = 4 # E: Cannot assign to final name "x"
371 d: D
372 d.x = 5 # E: Cannot assign to final attribute "x"
373 [out]
374
375 [case testFinalDelayedDefinitionOtherMethod]
376 from typing import Final
377
378 class C:
379 x: Final[int] # E: Final name must be initialized with a value
380
381 def meth(self) -> None:
382 self.x = 2 # E: Cannot assign to final attribute "x"
383 [out]
384
385 -- Reassignments
386
387 [case testFinalReassignModuleVar]
388 # flags: --allow-redefinition-old
389 from typing import Final
390
391 x: Final = 1
392 x
393 x = 2 # E: Cannot assign to final name "x"
394 def f() -> int:
395 global x
396 x = 3 # No error here is okay since we reported an error above
397 return x
398
399 x2: Final = 1
400 x2
401 def f2() -> None:
402 global x2
403 x2 = 1 # E: Cannot assign to final name "x2"
404
405 y = 1
406 y
407 y: Final = 2 # E: Cannot redefine an existing name as final
408 y = 3 # E: Cannot assign to final name "y"
409
410 z: Final = 1
411 z: Final = 2 # E: Cannot redefine an existing name as final
412 z = 3 # E: Cannot assign to final name "z"
413
414 [case testFinalReassignModuleVar2]
415 # flags: --allow-redefinition-old
416 from typing import Final
417
418 x: Final = 1
419 x
420 def f() -> int:
421 global x
422 x = 3 # E: Cannot assign to final name "x"
423 return x
424
425 y = 1
426 y
427 y = 2
428 y
429 y: Final = 3 # E: Cannot redefine an existing name as final
430
431 [case testFinalReassignModuleVar3]
432 # flags: --disallow-redefinition-old
433 from typing import Final
434
435 x: Final = 1
436 x
437 x = 2 # E: Cannot assign to final name "x"
438 def f() -> int:
439 global x
440 x = 3 # E: Cannot assign to final name "x"
441 return x
442
443 x2: Final = 1
444 x2
445 def f2() -> None:
446 global x2
447 x2 = 1 # E: Cannot assign to final name "x2"
448
449 y = 1 # E: Cannot assign to final name "y"
450 y
451 y: Final = 2 # E: Cannot redefine an existing name as final
452 y = 3 # E: Cannot assign to final name "y"
453
454 z: Final = 1
455 z: Final = 2 # E: Cannot redefine an existing name as final
456 z = 3 # E: Cannot assign to final name "z"
457
458 [case testFinalReassignModuleReexport]
459 from typing import Final
460
461 from lib import X
462 from lib.mod import ID
463
464 X = 1 # E: Cannot assign to final name "X"
465 ID: Final = 1 # E: Cannot redefine an existing name as final
466 ID = 1 # E: Cannot assign to final name "ID"
467 [file lib/__init__.pyi]
468 from lib.const import X as X
469
470 [file lib/mod.pyi]
471 from lib.const import *
472
473 [file lib/const.pyi]
474 from typing import Final
475
476 ID: Final # E: Type in Final[...] can only be omitted if there is an initializer
477 X: Final[int]
478
479 [case testFinalReassignFuncScope]
480 from typing import Final
481
482 def f() -> None:
483 nl: Final = 0
484 x: Final = 1
485 x = 1 # E: Cannot assign to final name "x"
486
487 y: Final = 1
488 y: Final = 2 # E: Cannot redefine an existing name as final
489 def nested() -> None:
490 nonlocal nl
491 nl = 1 # E: Cannot assign to final name "nl"
492 [out]
493
494 [case testFinalReassignModuleVarExternal]
495 import mod
496 mod.x = 2 # E: Cannot assign to final name "x"
497 [file mod.pyi]
498 from typing import Final
499 x: Final[int]
500 [out]
501
502 [case testFinalReassignInstanceVarClassBody]
503 from typing import Final
504
505 class C:
506 x: Final = 1
507 x = 2 # E: Cannot assign to final name "x"
508
509 y = 1 # E: Cannot assign to final name "y"
510 y: Final = 2 # E: Cannot redefine an existing name as final
511 [out]
512
513 [case testFinalReassignInstanceVarInit]
514 from typing import Final
515
516 class C:
517 def __init__(self) -> None:
518 self.x: Final = 1
519 self.y = 1
520 self.y: Final = 2 # E: Cannot redefine an existing name as final
521 def meth(self) -> None:
522 self.x = 2 # E: Cannot assign to final attribute "x"
523 [out]
524
525 [case testFinalReassignInstanceVarClassVsInit]
526
527 from typing import Final
528
529 class C:
530 y: Final = 1
531 def __init__(self) -> None:
532 # Methods are processed after top-level in new analyzer.
533 self.x: Final = 1 # E: Cannot redefine an existing name as final
534 self.y = 2 # E: Cannot assign to final attribute "y"
535 x = 2
536 [out]
537
538 [case testFinalReassignInstanceVarMethod]
539 from typing import Final
540
541 class C:
542 x: Final = 1
543 def __init__(self) -> None:
544 self.y: Final = 1
545 def meth(self) -> None:
546 self.x = 2 # E: Cannot assign to final attribute "x"
547 self.y = 2 # E: Cannot assign to final attribute "y"
548 def other(self) -> None:
549 self.x = 2 # E: Cannot assign to final attribute "x"
550 self.y = 2 # E: Cannot assign to final attribute "y"
551 @classmethod
552 def cm(cls) -> None:
553 cls.x = 2 # E: Cannot assign to final attribute "x"
554 cls.y # E: Cannot access final instance attribute "y" on class object
555 [builtins fixtures/classmethod.pyi]
556 [out]
557
558 [case testFinalReassignInstanceVarExternalClass]
559 from typing import Final
560
561 class C:
562 x: Final = 1
563 def __init__(self) -> None:
564 self.y: Final = 1
565
566 class D(C): pass
567
568 C.x = 2 # E: Cannot assign to final attribute "x"
569 D.x = 2 # E: Cannot assign to final attribute "x"
570 D.y = 2 # E: Cannot access final instance attribute "y" on class object \
571 # E: Cannot assign to final attribute "y"
572 [out]
573
574 [case testFinalReassignInstanceVarExternalInstance]
575 from typing import Final
576
577 class C:
578 x: Final = 1
579 def __init__(self) -> None:
580 self.y: Final = 1
581
582 class D(C): pass
583
584 C().x = 2 # E: Cannot assign to final attribute "x"
585 D().x = 2 # E: Cannot assign to final attribute "x"
586 D().y = 2 # E: Cannot assign to final attribute "y"
587 [out]
588
589 [case testFinalWorksWithComplexTargets]
590 from typing import Final, Any
591
592 y: Final[Any] = 1
593 x = a, (b, y), c = 2, (2, 2), 2 # E: Cannot assign to final name "y"
594 t, *y, s = u = [2, 2, 2] # E: Cannot assign to final name "y"
595 [builtins fixtures/list.pyi]
596 [out]
597
598 [case testFinalInplaceAssign]
599 from typing import Final
600
601 class A: # no such things in fixtures
602 def __add__(self, other: A) -> A: ...
603 class B:
604 def __add__(self, other: B) -> B: ...
605 def __iadd__(self, other: B) -> B: ...
606
607 a: Final = A()
608 b: Final = B()
609 class C:
610 a: Final = A()
611 b: Final = B()
612 class D(C):
613 pass
614
615 a += A() # E: Cannot assign to final name "a"
616 b += B() # E: Cannot assign to final name "b"
617 D().a += A() # E: Cannot assign to final attribute "a"
618 D().b += B() # E: Cannot assign to final attribute "b"
619 [out]
620
621 -- Overriding
622
623 [case testFinalOverridingVarClassBody]
624 from typing import Final
625
626 # We use properties in this tests and below because we want to check
627 # that any existing variable before final doesn't affect logic of
628 # subsequent overrides but writable attributes cannot be overridden by final.
629 class A:
630 @property
631 def x(self) -> int: ...
632 @property
633 def y(self) -> int: ...
634
635 class B(A):
636 x: Final = 1
637 def __init__(self) -> None:
638 self.y: Final = 1
639 class C(B):
640 x: int = 2 # E: Cannot assign to final name "x"
641 y: int = 2 # E: Cannot assign to final name "y"
642 x = 3 # E: Cannot assign to final name "x"
643 y = 3 # E: Cannot assign to final name "y"
644 class D(C):
645 pass
646 D.x = 4 # E: Cannot assign to final attribute "x"
647 D.y = 4 # E: Cannot assign to final attribute "y"
648 [builtins fixtures/property.pyi]
649 [out]
650
651 [case testFinalOverridingVarClassBodyExplicit]
652 from typing import Final
653
654 class A:
655 @property
656 def x(self) -> int: ...
657 @property
658 def y(self) -> int: ...
659 class B(A):
660 x: Final = 1
661 def __init__(self) -> None:
662 self.y: Final = 1
663 class C(B):
664 x: Final = 2 # E: Cannot override final attribute "x" (previously declared in base class "B")
665 y: Final = 2 # E: Cannot override final attribute "y" (previously declared in base class "B")
666 [builtins fixtures/property.pyi]
667 [out]
668
669 [case testFinalOverridingVarInit]
670 from typing import Final
671
672 class A:
673 @property
674 def x(self) -> int: ...
675 @property
676 def y(self) -> int: ...
677 class B(A):
678 x: Final = 1
679 def __init__(self) -> None:
680 self.y: Final = 1
681 class C(B):
682 def __init__(self) -> None:
683 self.x = 2 # E: Cannot assign to final attribute "x"
684 self.y = 2 # E: Cannot assign to final attribute "y"
685 def meth(self) -> None:
686 self.x = 3 # E: Cannot assign to final attribute "x"
687 self.y = 3 # E: Cannot assign to final attribute "y"
688 [builtins fixtures/property.pyi]
689 [out]
690
691 [case testFinalOverridingVarInit2]
692 from typing import Final
693
694 class A:
695 @property
696 def x(self) -> int: ...
697 @property
698 def y(self) -> int: ...
699 class B(A):
700 x: Final = 1
701 def __init__(self) -> None:
702 self.y: Final = 1
703 class C(B):
704 def __init__(self) -> None:
705 self.x: Final = 2 # E: Cannot override final attribute "x" (previously declared in base class "B")
706 self.y: Final = 2 # E: Cannot override final attribute "y" (previously declared in base class "B")
707 [builtins fixtures/property.pyi]
708 [out]
709
710 [case testFinalOverridingVarOtherMethod]
711 from typing import Final
712
713 class A:
714 @property
715 def x(self) -> int: ...
716 @property
717 def y(self) -> int: ...
718 class B(A):
719 x: Final = 1
720 def __init__(self) -> None:
721 self.y: Final = 1
722 class C(B):
723 def meth(self) -> None:
724 self.x: int = 2 # E: Cannot assign to final attribute "x"
725 self.y: int = 2 # E: Cannot assign to final attribute "y"
726
727 self.x = 3 # E: Cannot assign to final attribute "x"
728 self.y = 3 # E: Cannot assign to final attribute "y"
729 [builtins fixtures/property.pyi]
730 [out]
731
732 [case testFinalOverridingVarMultipleInheritanceClass]
733 from typing import Final, Any
734
735 class A:
736 x: Final[Any] = 1
737 class B:
738 @property
739 def x(self) -> int: ...
740 class C(A, B): ...
741 class D(B, A): ... # E: Cannot override final attribute "x" (previously declared in base class "A")
742 C.x = 3 # E: Cannot assign to final attribute "x"
743 C().x = 4 # E: Cannot assign to final attribute "x"
744 D().x = 4 # E: Cannot assign to final attribute "x" \
745 # E: Property "x" defined in "B" is read-only
746 [builtins fixtures/property.pyi]
747 [out]
748
749 [case testFinalOverridingVarMultipleInheritanceInit]
750 from typing import Final, Any
751
752 class A:
753 def __init__(self) -> None:
754 self.x: Final[Any] = 1
755 class B:
756 @property
757 def x(self) -> int: ...
758 class C(A, B): ...
759 class D(B, A): ... # E: Cannot override final attribute "x" (previously declared in base class "A")
760 C.x = 3 # E: Cannot access final instance attribute "x" on class object \
761 # E: Cannot assign to final attribute "x"
762 C().x = 4 # E: Cannot assign to final attribute "x"
763 [builtins fixtures/property.pyi]
764 [out]
765
766 [case testFinalOverridingVarMultipleInheritanceMixed]
767 from typing import Final
768
769 class A:
770 x: Final = 1
771 class B:
772 def __init__(self) -> None:
773 self.x = 2
774 class C(A, B): ... # E: Cannot override writable attribute "x" with a final one
775 class D(B, A): ... # E: Cannot override final attribute "x" (previously declared in base class "A")
776 C.x = 3 # E: Cannot assign to final attribute "x"
777 D.x = 3 # E: Cannot assign to final attribute "x"
778 C().x = 4 # E: Cannot assign to final attribute "x"
779 D().x = 4 # E: Cannot assign to final attribute "x"
780 [out]
781
782 [case testFinalOverridingVarWithMethod]
783 from typing import Final, Any
784
785 class A:
786 x: Final[Any] = 1
787 def __init__(self) -> None:
788 self.y: Final[Any] = 1
789
790 class B(A):
791 def x(self) -> None: pass # E: Cannot override final attribute "x" (previously declared in base class "A")
792 def y(self) -> None: pass # E: Cannot override final attribute "y" (previously declared in base class "A")
793
794 class C(A):
795 @property # E: Cannot override final attribute "x" (previously declared in base class "A")
796 def x(self) -> None: pass
797 @property # E: Cannot override final attribute "y" (previously declared in base class "A")
798 def y(self) -> None: pass
799 [builtins fixtures/property.pyi]
800 [out]
801
802 [case testFinalOverridingVarWithMethodClass]
803 from typing import Final, Any
804
805 class A:
806 x: Final[Any] = 1
807 def __init__(self) -> None:
808 self.y: Final[Any] = 1
809
810 class B(A):
811 @classmethod # E: Cannot override final attribute "x" (previously declared in base class "A")
812 def x(self) -> None: pass
813 @classmethod # E: Cannot override final attribute "y" (previously declared in base class "A")
814 def y(self) -> None: pass
815
816 [builtins fixtures/classmethod.pyi]
817 [out]
818
819 [case testFinalOverridingMethodRegular]
820 from typing import final
821
822 class B:
823 @final
824 def meth(self) -> None: ...
825 class C(B):
826 def meth(self) -> None: ... # E: Cannot override final attribute "meth" (previously declared in base class "B")
827 [out]
828
829 [case testFinalOverridingMethodInitNew]
830 from typing import final
831
832 class B:
833 @final
834 def __init__(self) -> None: ...
835 @final
836 def __new__(cls) -> B: ...
837 class C(B):
838 def __init__(self) -> None: ... # E: Cannot override final attribute "__init__" (previously declared in base class "B")
839 def __new__(cls) -> C: ... # E: Cannot override final attribute "__new__" (previously declared in base class "B")
840 [out]
841
842 [case testFinalOverridingMethodWithVar]
843 from typing import final, Final, Any
844
845 a: Any
846
847 class A:
848 @final
849 def f(self) -> None: pass
850 @final
851 @property
852 def p(self) -> int: pass
853
854 class B(A):
855 f = a # E: Cannot override final attribute "f" (previously declared in base class "A")
856 p = a # E: Cannot override final attribute "p" (previously declared in base class "A")
857 class C(A):
858 f: Any # E: Cannot override final attribute "f" (previously declared in base class "A")
859 p: Any # E: Cannot override final attribute "p" (previously declared in base class "A")
860 class D(A):
861 f: Final = a # E: Cannot override final attribute "f" (previously declared in base class "A")
862 p: Final = a # E: Cannot override final attribute "p" (previously declared in base class "A")
863 [builtins fixtures/property.pyi]
864 [out]
865
866 [case testFinalOverridingMethodWithVarImplicit]
867 from typing import final, Any, Final
868
869 a: Any
870
871 class A:
872 @final
873 def f(self) -> None: pass
874 @final
875 @classmethod
876 def c(cls) -> int: pass
877
878 class B(A):
879 def __init__(self) -> None:
880 self.f: Any # E: Cannot assign to final attribute "f" \
881 # E: Cannot override final attribute "f" (previously declared in base class "A")
882 self.c: Any # E: Cannot assign to final attribute "c" \
883 # E: Cannot override final attribute "c" (previously declared in base class "A")
884
885 B().f = a # E: Cannot assign to final attribute "f"
886 B().c = a # E: Cannot assign to final attribute "c"
887
888 class C(A):
889 def __init__(self) -> None:
890 self.f: Final = a # E: Cannot override final attribute "f" (previously declared in base class "A")
891 self.c: Final = a # E: Cannot override final attribute "c" (previously declared in base class "A")
892 [builtins fixtures/classmethod.pyi]
893 [out]
894
895 [case testFinalCanOverrideMethodWithFinal]
896 from typing import final
897
898 class B:
899 def meth(self) -> None: ...
900 class C(B):
901 @final # OK
902 def meth(self) -> None: ...
903 [out]
904
905 [case testFinalOverridingMethodMultipleInheritance]
906 from typing import final
907
908 class A:
909 def m(self) -> int: pass
910 class B:
911 @final
912 def m(self) -> int: pass
913
914 class C(A, B): pass # E: Cannot override final attribute "m" (previously declared in base class "B")
915 class D(B, A): pass
916 [out]
917
918 [case testFinalOverridingMethodMultipleInheritanceVar]
919 from typing import final, Any
920
921 class A:
922 m: Any
923 class B:
924 @final
925 def m(self) -> int: pass
926
927 class C(A, B): pass # E: Cannot override final attribute "m" (previously declared in base class "B")
928 class D(B, A): pass # E: Cannot override writable attribute "m" with a final one
929 [out]
930
931 [case testFinalOverridingClassMethod]
932 from typing import final
933
934 class B:
935 @classmethod
936 @final
937 def f(cls) -> int: pass
938
939 class C(B):
940 @classmethod # E: Cannot override final attribute "f" (previously declared in base class "B")
941 def f(cls) -> int: pass
942 [builtins fixtures/classmethod.pyi]
943 [out]
944
945 [case testFinalOverridingStaticMethod]
946 from typing import final
947
948 class B:
949 @staticmethod
950 @final
951 def f() -> int: pass
952 @final
953 @staticmethod
954 def g() -> int: pass
955
956 class C(B):
957 @staticmethod # E: Cannot override final attribute "f" (previously declared in base class "B")
958 def f() -> int: pass
959 @staticmethod # E: Cannot override final attribute "g" (previously declared in base class "B")
960 def g() -> int: pass
961 [builtins fixtures/staticmethod.pyi]
962 [out]
963
964 [case testFinalOverridingProperty]
965 from typing import final
966
967 class B:
968 @final
969 @property
970 def f(self) -> int: pass
971 @property
972 @final
973 def g(self) -> int: pass
974
975 class C(B):
976 @property # E: Cannot override final attribute "f" (previously declared in base class "B")
977 def f(self) -> int: pass
978 @property # E: Cannot override final attribute "g" (previously declared in base class "B")
979 def g(self) -> int: pass
980 [builtins fixtures/property.pyi]
981 [out]
982
983 [case testFinalOverridingMethodOverloads]
984 from typing import final, overload
985
986 class B:
987 @overload
988 def f(self, x: int) -> int: ...
989 @overload
990 def f(self, x: str) -> str: ...
991 @final
992 def f(self, x):
993 pass
994
995 class C(B):
996 @overload # E: Cannot override final attribute "f" (previously declared in base class "B")
997 def f(self, x: int) -> int: ...
998 @overload
999 def f(self, x: str) -> str: ...
1000 def f(self, x):
1001 pass
1002 [out]
1003
1004 [case testFinalClassNoInheritance]
1005 from typing import final
1006
1007 @final
1008 class B: ...
1009 class C(B): # E: Cannot inherit from final class "B"
1010 pass
1011 class D(C): # E: Cannot inherit from final class "B"
1012 pass
1013 [out]
1014
1015 [case testFinalClassNoInheritanceMulti]
1016 from typing import final
1017
1018 class A: ...
1019 @final
1020 class B: ...
1021 class C(B, A): # E: Cannot inherit from final class "B"
1022 pass
1023 class D(A, B): # E: Cannot inherit from final class "B"
1024 pass
1025 [out]
1026
1027 [case testFinalCantOverrideWriteable]
1028 from typing import Any, Final, final
1029
1030 class B:
1031 x: Any
1032 @property
1033 def y(self) -> Any: ...
1034 @y.setter
1035 def y(self, x: Any) -> None: ...
1036
1037 class C(B):
1038 x: Final = 1 # E: Cannot override writable attribute "x" with a final one
1039 y: Final = 1 # E: Cannot override writable attribute "y" with a final one
1040
1041 class D(B):
1042 @final # E: Cannot override writable attribute "x" with a final one
1043 def x(self) -> int: ...
1044 @final # E: Cannot override writable attribute "y" with a final one
1045 def y(self) -> int: ...
1046 [builtins fixtures/property.pyi]
1047 [out]
1048
1049 [case testFinalCanUseTypingExtensions]
1050 from typing_extensions import final, Final
1051
1052 x: Final = 1
1053 x = 2 # E: Cannot assign to final name "x"
1054
1055 class S:
1056 x: Final = 1
1057 S.x = 2 # E: Cannot assign to final attribute "x"
1058
1059 class B:
1060 @final
1061 def meth(self) -> None: ...
1062 class C(B):
1063 def meth(self) -> None: ... # E: Cannot override final attribute "meth" (previously declared in base class "B")
1064
1065 @final
1066 class F: ...
1067 class E(F): ... # E: Cannot inherit from final class "F"
1068 [builtins fixtures/tuple.pyi]
1069 [out]
1070
1071 [case testFinalCanUseTypingExtensionsAliased]
1072 from typing_extensions import final as f, Final as F
1073
1074 x: F = 1
1075 x = 2 # E: Cannot assign to final name "x"
1076
1077 class S:
1078 x: F = 1
1079 S.x = 2 # E: Cannot assign to final attribute "x"
1080
1081 class B:
1082 @f
1083 def meth(self) -> None: ...
1084 class C(B):
1085 def meth(self) -> None: ... # E: Cannot override final attribute "meth" (previously declared in base class "B")
1086
1087 @f
1088 class D(C): ...
1089 class E(D): ... # E: Cannot inherit from final class "D"
1090 [builtins fixtures/tuple.pyi]
1091 [out]
1092
1093 [case testFinalMultiassignAllowed]
1094 from typing import Final
1095
1096 class A:
1097 x: Final[int]
1098 y: Final[int]
1099 def __init__(self) -> None:
1100 self.x, self.y = 1, 2
1101
1102 class B:
1103 x: Final[int]
1104 y: Final[int]
1105 def __init__(self) -> None:
1106 self.x = self.y = 1
1107 [out]
1108
1109 [case testFinalInDeferredMethod]
1110 from typing import Final
1111
1112 class A:
1113 def __init__(self) -> None:
1114 self.x = 10 # type: Final
1115 undefined # type: ignore
1116 [builtins fixtures/tuple.pyi]
1117
1118 [case testFinalUsedWithClassVar]
1119 # flags: --python-version 3.12
1120 from typing import Final, ClassVar
1121
1122 class A:
1123 a: Final[ClassVar[int]] # E: Variable should not be annotated with both ClassVar and Final
1124 b: ClassVar[Final[int]] # E: Final can be only used as an outermost qualifier in a variable annotation
1125 c: ClassVar[Final] = 1 # E: Final can be only used as an outermost qualifier in a variable annotation
1126 [out]
1127
1128 [case testFinalUsedWithClassVarAfterPy313]
1129 # flags: --python-version 3.13
1130 from typing import Final, ClassVar
1131
1132 class A:
1133 a: Final[ClassVar[int]] = 1
1134 b: ClassVar[Final[int]] = 1
1135 c: ClassVar[Final] = 1
1136
1137 [case testFinalClassWithAbstractMethod]
1138 from typing import final
1139 from abc import ABC, abstractmethod
1140
1141 @final
1142 class A(ABC): # E: Final class __main__.A has abstract attributes "B"
1143 @abstractmethod
1144 def B(self) -> None: ...
1145
1146 [case testFinalDefiningFuncWithAbstractMethod]
1147 from typing import final
1148 from abc import ABC, abstractmethod
1149
1150 class A(ABC):
1151 @final # E: Method B is both abstract and final
1152 @abstractmethod
1153 def B(self) -> None: ...
1154
1155 [case testFinalClassVariableRedefinitionDoesNotCrash]
1156 # This used to crash -- see #12950
1157 from typing import Final
1158
1159 class MyClass:
1160 a: None
1161 a: Final[int] = 1 # E: Cannot redefine an existing name as final # E: Name "a" already defined on line 5
1162
1163 [case testFinalOverrideAllowedForPrivate]
1164 from typing import Final, final
1165
1166 class Parent:
1167 __foo: Final[int] = 0
1168 @final
1169 def __bar(self) -> None: ...
1170
1171 class Child(Parent):
1172 __foo: Final[int] = 1
1173 @final
1174 def __bar(self) -> None: ...
1175
1176 [case testFinalWithoutBool]
1177 from typing import Literal, final
1178
1179 class A:
1180 pass
1181
1182 @final
1183 class B:
1184 pass
1185
1186 @final
1187 class C:
1188 def __len__(self) -> Literal[1]: return 1
1189
1190 reveal_type(A() and 42) # N: Revealed type is "__main__.A | Literal[42]?"
1191 reveal_type(B() and 42) # N: Revealed type is "Literal[42]?"
1192 reveal_type(C() and 42) # N: Revealed type is "Literal[42]?"
1193
1194 [builtins fixtures/bool.pyi]
1195
1196 [case testFinalWithoutBoolButWithLen]
1197 from typing import Literal, final
1198
1199 # Per Python data model, __len__ is called if __bool__ does not exist.
1200 # In a @final class, __bool__ would not exist.
1201
1202 @final
1203 class A:
1204 def __len__(self) -> int: ...
1205
1206 @final
1207 class B:
1208 def __len__(self) -> Literal[1]: return 1
1209
1210 @final
1211 class C:
1212 def __len__(self) -> Literal[0]: return 0
1213
1214 reveal_type(A() and 42) # N: Revealed type is "__main__.A | Literal[42]?"
1215 reveal_type(B() and 42) # N: Revealed type is "Literal[42]?"
1216 reveal_type(C() and 42) # N: Revealed type is "__main__.C"
1217
1218 [builtins fixtures/bool.pyi]
1219
1220 [case testCanAccessFinalClassInit]
1221 from typing import final
1222
1223 @final
1224 class FinalClass:
1225 pass
1226
1227 def check_final_class() -> None:
1228 new_instance = FinalClass()
1229 new_instance.__init__()
1230
1231 class FinalInit:
1232 @final
1233 def __init__(self) -> None:
1234 pass
1235
1236 def check_final_init() -> None:
1237 new_instance = FinalInit()
1238 new_instance.__init__()
1239 [builtins fixtures/tuple.pyi]
1240
1241 [case testNarrowingOfFinalInFunctionsNotSpecial]
1242 from typing import Final, Union
1243
1244 def _init() -> Union[int, None]:
1245 return 0
1246
1247 FOO: Final = _init()
1248
1249 class Example:
1250 if FOO is not None:
1251 reveal_type(FOO) # N: Revealed type is "builtins.int"
1252
1253 def fn(self) -> int:
1254 return FOO # E: Incompatible return value type (got "int | None", expected "int")
1255
1256 if FOO is not None:
1257 reveal_type(FOO) # N: Revealed type is "builtins.int"
1258
1259 def func() -> int:
1260 return FOO # E: Incompatible return value type (got "int | None", expected "int")
1261
1262 [case testDisjointBase]
1263 from typing_extensions import disjoint_base
1264
1265 @disjoint_base
1266 class Disjoint1: pass
1267
1268 @disjoint_base
1269 class Disjoint2: pass
1270
1271 @disjoint_base
1272 class DisjointChild(Disjoint1): pass
1273
1274 class C1: pass
1275 class C2(Disjoint1, C1): pass
1276 class C3(DisjointChild, Disjoint1): pass
1277
1278 class C4(Disjoint1, Disjoint2): # E: Class "C4" has incompatible disjoint bases
1279 pass
1280
1281 class C5(Disjoint2, Disjoint1): # E: Class "C5" has incompatible disjoint bases
1282 pass
1283
1284 class C6(Disjoint2, DisjointChild): # E: Class "C6" has incompatible disjoint bases
1285 pass
1286
1287 class C7(DisjointChild, Disjoint2): # E: Class "C7" has incompatible disjoint bases
1288 pass
1289
1290 class C8(DisjointChild, Disjoint1, Disjoint2): # E: Class "C8" has incompatible disjoint bases
1291 pass
1292
1293 class C9(C2, Disjoint2): # E: Class "C9" has incompatible disjoint bases
1294 pass
1295
1296 class C10(C3, Disjoint2): # E: Class "C10" has incompatible disjoint bases
1297 pass
1298
1299 [builtins fixtures/tuple.pyi]
1300 [case testDisjointBaseSlots]
1301 class S1:
1302 __slots__ = ("a",)
1303
1304 class S2:
1305 __slots__ = ("b",)
1306
1307 class S3:
1308 __slots__ = ()
1309
1310 class S4(S1):
1311 __slots__ = ("c",)
1312
1313 class S5(S1, S2): # E: Class "S5" has incompatible disjoint bases
1314 pass
1315
1316 class S6(S1, S3): pass # OK
1317 class S7(S3, S1): pass # OK
1318
1319 class S8(S4, S1): pass # OK
1320
1321 class S9(S2, S4): # E: Class "S9" has incompatible disjoint bases
1322 pass
1323
1324 [builtins fixtures/tuple.pyi]