forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-final.test
More file actions
1069 lines (874 loc) · 28.9 KB
/
check-final.test
File metadata and controls
1069 lines (874 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Test cases for final qualifier
--
-- Definitions
[case testFinalDefiningModuleVar]
from typing import Final
x: Final = int()
y: Final[float] = int()
z: Final[int] = int()
bad: Final[str] = int() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
reveal_type(x) # E: Revealed type is 'builtins.int'
reveal_type(y) # E: Revealed type is 'builtins.float'
reveal_type(z) # E: Revealed type is 'builtins.int'
[out]
[case testFinalDefiningInstanceVar]
from typing import Final
class C:
x: Final = int()
y: Final[float] = int()
z: Final[int] = int()
bad: Final[str] = int() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
class D(C): pass
reveal_type(D.x) # E: Revealed type is 'builtins.int'
reveal_type(D.y) # E: Revealed type is 'builtins.float'
reveal_type(D.z) # E: Revealed type is 'builtins.int'
reveal_type(D().x) # E: Revealed type is 'builtins.int'
reveal_type(D().y) # E: Revealed type is 'builtins.float'
reveal_type(D().z) # E: Revealed type is 'builtins.int'
[out]
[case testFinalDefiningInstanceVarImplicit]
from typing import Final, Tuple, Any
class C:
def __init__(self, x: Tuple[int, Any]) -> None:
self.x: Final = x
self.y: Final[float] = 1
reveal_type(C((1, 2)).x) # E: Revealed type is 'Tuple[builtins.int, Any]'
reveal_type(C((1, 2)).y) # E: Revealed type is 'builtins.float'
[out]
[case testFinalBadDefinitionTooManyArgs]
from typing import Final
x: Final[int, str] # E: Final name must be initialized with a value \
# E: Final[...] takes at most one type argument
reveal_type(x) # E: Revealed type is 'builtins.int'
class C:
def __init__(self) -> None:
self.x: Final[float, float] = 1 # E: Final[...] takes at most one type argument
reveal_type(C().x) # E: Revealed type is 'builtins.float'
[out]
[case testFinalInvalidDefinitions]
# flags: --no-new-semantic-analyzer
# Errors are shown in a different order with the new analyzer.
from typing import Final, Any
x = y = 1 # type: Final[float] # E: Invalid final declaration
z: Any
z[0]: Final[int] # E: Unexpected type declaration \
# E: Invalid final declaration
[out]
[case testFinalDefiningInstanceVarStubs]
# Allow skipping r.h.s.
import mod
[file mod.pyi]
from typing import Final
x: Final # E: Type in Final[...] can only be omitted if there is an initializer
y: Final[int]
class C:
x: Final # E: Type in Final[...] can only be omitted if there is an initializer
y: Final[int]
def __init__(self) -> None:
self.z: Final # E: Type in Final[...] can only be omitted if there is an initializer
reveal_type(x) # E: Revealed type is 'Any'
reveal_type(C.x) # E: Revealed type is 'Any'
v: C
reveal_type(v.z) # E: Revealed type is 'Any'
[out]
[case testFinalDefiningFunc]
from typing import final
@final # E: @final cannot be used with non-method functions
def f(x: int) -> None: ...
[out]
[case testFinalDefiningFuncOverloaded]
from typing import final, overload
@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
@final # E: @final cannot be used with non-method functions
def f(x):
pass
[out]
[case testFinalDefiningMeth]
from typing import final
class C:
@final
def f(self, x: int) -> None: ...
reveal_type(C().f) # E: Revealed type is 'def (x: builtins.int)'
[out]
[case testFinalDefiningMethOverloaded]
from typing import final, overload
class C:
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
@final
def f(self, x):
pass
@overload
def bad(self, x: int) -> int: ...
@final # E: @final should be applied only to overload implementation
@overload
def bad(self, x: str) -> str: ...
def bad(self, x):
pass
reveal_type(C().f) # E: Revealed type is 'Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)'
[out]
[case testFinalDefiningMethOverloadedStubs]
from mod import C
reveal_type(C().f)
[file mod.pyi]
from typing import final, overload
class C:
@final
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
@overload
def bad(self, x: int) -> int: ...
@final # Error!
@overload
def bad(self, x: str) -> str: ...
[out]
tmp/mod.pyi:12: error: In a stub file @final must be applied only to the first overload
main:3: error: Revealed type is 'Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)'
[case testFinalDefiningProperty]
from typing import final
class C:
@final
@property
def f(self) -> int: pass
@property
@final
def g(self) -> int: pass
reveal_type(C().f) # E: Revealed type is 'builtins.int'
reveal_type(C().g) # E: Revealed type is 'builtins.int'
[builtins fixtures/property.pyi]
[out]
[case testFinalDefiningOuterOnly]
from typing import Final, Callable, Tuple, Any
x: Tuple[Final] # E: Final can be only used as an outermost qualifier in a variable annotation
y: Callable[[], Tuple[Final[int]]] # E: Final can be only used as an outermost qualifier in a variable annotation
[out]
[case testFinalDefiningNotInMethod]
from typing import Final
def f(x: Final[int]) -> int: ... # E: Final can be only used as an outermost qualifier in a variable annotation
def g(x: int) -> Final[int]: ... # E: Final can be only used as an outermost qualifier in a variable annotation
[out]
[case testFinalDefiningNotInMethodExtensions]
from typing_extensions import Final
def f(x: Final[int]) -> int: ... # E: Final can be only used as an outermost qualifier in a variable annotation
def g(x: int) -> Final[int]: ... # E: Final can be only used as an outermost qualifier in a variable annotation
[out]
[case testFinalDefiningNoRhs]
from typing import Final
x: Final # E: Type in Final[...] can only be omitted if there is an initializer
y: Final[int] # E: Final name must be initialized with a value
class C:
x: Final # E: Type in Final[...] can only be omitted if there is an initializer
y: Final[int] # E: Final name must be initialized with a value
def __init__(self) -> None:
self.z: Final # E: Type in Final[...] can only be omitted if there is an initializer
reveal_type(x) # E: Revealed type is 'Any'
reveal_type(y) # E: Revealed type is 'builtins.int'
reveal_type(C().x) # E: Revealed type is 'Any'
reveal_type(C().y) # E: Revealed type is 'builtins.int'
reveal_type(C().z) # E: Revealed type is 'Any'
[out]
[case testFinalDefiningNoRhsSubclass]
from typing import Final
class A:
x: Final[int] # E: Final name must be initialized with a value
class B(A):
x = 1 # E: Cannot assign to final name "x"
def __init__(self) -> None:
self.x = 1 # E: Cannot assign to final attribute "x"
[out]
[case testFinalDefiningNoTypevarsExplicit]
from typing import Final, TypeVar, Generic, Tuple, Any
T = TypeVar('T')
d: Any
class C(Generic[T]):
x: Final[Tuple[T, T]] = d # E: Final name declared in class body cannot depend on type variables
[out]
[case testFinalDefiningTypevarsImplicit]
from typing import Final, TypeVar, Generic, Tuple, Any
T = TypeVar('T')
class C(Generic[T]):
def __init__(self, x: Tuple[T, T]) -> None:
self.x: Final = x
self.y: Final = 1
reveal_type(C((1, 2)).x) # E: Revealed type is 'Tuple[builtins.int*, builtins.int*]'
C.x # E: Cannot access final instance attribute "x" on class object \
# E: Access to generic instance variables via class is ambiguous
C.y # E: Cannot access final instance attribute "y" on class object
[out]
[case testFinalDefiningNotInOtherMethod]
from typing import Final, Any, Tuple
class C:
def meth(self, x: Tuple[int, Any]) -> None:
self.x: Final = x # E: Can only declare a final attribute in class body or __init__
self.y: Final[float] = 1 # E: Can only declare a final attribute in class body or __init__
[out]
[case testFinalDefiningOnlyOnSelf]
from typing import Final, Any, Tuple
class U:
x: Any
y: Any
class C:
def __init__(self, x: Tuple[int, Any]) -> None:
slf = U()
slf.x: Final = x # E: Final can be only applied to a name or an attribute on self
slf.y: Final[float] = 1 # E: Type cannot be declared in assignment to non-self attribute \
# E: Final can be only applied to a name or an attribute on self
[out]
[case testFinalNotInProtocol]
from typing import Final, final, Protocol, overload
class P(Protocol):
x: Final[float] = 1 # E: Protocol member cannot be final
@final # E: Protocol member cannot be final
def meth(self, x) -> int:
pass
@overload
def other(self, x: int) -> int: ...
@overload
def other(self, x: str) -> str: ...
@final # E: Protocol member cannot be final
def other(self, x):
pass
[out]
[case testFinalNotInLoops]
from typing import Final
for i in [1, 2, 3]:
x: Final = i # E: Cannot use Final inside a loop
while True:
y: Final = True # E: Cannot use Final inside a loop
[builtins fixtures/list.pyi]
[out]
[case testFinalDelayedDefinition]
from typing import Final
class C:
x: Final[int] # OK, defined in __init__
bad: Final[int] # E: Final name must be initialized with a value
def __init__(self, x: int) -> None:
self.x = x # OK, deferred definition
self.x = 2 # E: Cannot assign to final attribute "x"
def meth(self) -> None:
self.x = 2 # E: Cannot assign to final attribute "x"
c: C
c.x = 3 # E: Cannot assign to final attribute "x"
class D(C):
x = 4 # E: Cannot assign to final name "x"
d: D
d.x = 5 # E: Cannot assign to final attribute "x"
[out]
[case testFinalDelayedDefinitionOtherMethod]
from typing import Final
class C:
x: Final[int] # E: Final name must be initialized with a value
def meth(self) -> None:
self.x = 2 # E: Cannot assign to final attribute "x"
[out]
-- Reassignments
[case testFinalReassignModuleVar]
# flags: --allow-redefinition --new-semantic-analyzer
from typing import Final
x: Final = 1
x
x = 2 # E: Cannot assign to final name "x"
def f() -> int:
global x
x = 3 # No error here is okay since we reported an error above
return x
x2: Final = 1
x2
def f2() -> None:
global x2
x2 = 1 # E: Cannot assign to final name "x2"
y = 1
y
y: Final = 2 # E: Cannot redefine an existing name as final
y = 3 # E: Cannot assign to final name "y"
z: Final = 1
z: Final = 2 # E: Cannot redefine an existing name as final
z = 3 # E: Cannot assign to final name "z"
[case testFinalReassignModuleVar2]
# flags: --allow-redefinition
from typing import Final
x: Final = 1
x
def f() -> int:
global x
x = 3 # E: Cannot assign to final name "x"
return x
y = 1
y
y = 2
y
y: Final = 3 # E: Cannot redefine an existing name as final
[case testFinalReassignModuleVar3]
# flags: --disallow-redefinition --no-new-semantic-analyzer
# Error formatting is subtly different with new analyzer.
from typing import Final
x: Final = 1
x
x = 2 # E: Cannot assign to final name "x"
def f() -> int:
global x
x = 3 # E: Cannot assign to final name "x"
return x
x2: Final = 1
x2
def f2() -> None:
global x2
x2 = 1 # E: Cannot assign to final name "x2"
y = 1 # E: Cannot assign to final name "y"
y
y: Final = 2 # E: Name 'y' already defined on line 19 \
# E: Cannot redefine an existing name as final
y = 3 # E: Cannot assign to final name "y"
z: Final = 1
z: Final = 2 # E: Name 'z' already defined on line 24 \
# E: Cannot redefine an existing name as final
z = 3 # E: Cannot assign to final name "z"
[case testFinalReassignModuleReexport]
# flags: --no-new-semantic-analyzer
# Error formatting is subtly different with the new analyzer.
from typing import Final
from lib import X
from lib.mod import ID
X = 1 # Error!
ID: Final = 1 # Two errors!
ID = 1 # Error!
[file lib/__init__.pyi]
from lib.const import X as X
[file lib/mod.pyi]
from lib.const import *
[file lib/const.pyi]
from typing import Final
ID: Final # Error!
X: Final[int]
[out]
tmp/lib/const.pyi:3: error: Type in Final[...] can only be omitted if there is an initializer
main:8: error: Cannot assign to final name "X"
main:9: error: Name 'ID' already defined (possibly by an import)
main:9: error: Cannot redefine an existing name as final
main:10: error: Cannot assign to final name "ID"
[case testFinalReassignFuncScope]
from typing import Final
def f() -> None:
nl: Final = 0
x: Final = 1
x = 1 # E: Cannot assign to final name "x"
y: Final = 1
y: Final = 2 # E: Cannot redefine an existing name as final
def nested() -> None:
nonlocal nl
nl = 1 # E: Cannot assign to final name "nl"
[out]
[case testFinalReassignModuleVarExternal]
import mod
mod.x = 2 # E: Cannot assign to final name "x"
[file mod.pyi]
from typing import Final
x: Final[int]
[out]
[case testFinalReassignInstanceVarClassBody]
from typing import Final
class C:
x: Final = 1
x = 2 # E: Cannot assign to final name "x"
y = 1 # E: Cannot assign to final name "y"
y: Final = 2 # E: Cannot redefine an existing name as final
[out]
[case testFinalReassignInstanceVarInit]
from typing import Final
class C:
def __init__(self) -> None:
self.x: Final = 1
self.y = 1
self.y: Final = 2 # E: Cannot redefine an existing name as final
def meth(self) -> None:
self.x = 2 # E: Cannot assign to final attribute "x"
[out]
[case testFinalReassignInstanceVarClassVsInit]
# flags: --no-new-semantic-analyzer
# Methods are processed after top-level in new analyzer.
from typing import Final
class C:
y: Final = 1
def __init__(self) -> None:
self.x: Final = 1
self.y = 2 # E: Cannot assign to final attribute "y"
x = 2 # E: Cannot assign to final name "x"
[out]
[case testFinalReassignInstanceVarMethod]
from typing import Final
class C:
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
def meth(self) -> None:
self.x = 2 # E: Cannot assign to final attribute "x"
self.y = 2 # E: Cannot assign to final attribute "y"
def other(self) -> None:
self.x = 2 # E: Cannot assign to final attribute "x"
self.y = 2 # E: Cannot assign to final attribute "y"
@classmethod
def cm(cls) -> None:
cls.x = 2 # E: Cannot assign to final attribute "x"
cls.y # E: Cannot access final instance attribute "y" on class object
[builtins fixtures/classmethod.pyi]
[out]
[case testFinalReassignInstanceVarExternalClass]
from typing import Final
class C:
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class D(C): pass
C.x = 2 # E: Cannot assign to final attribute "x"
D.x = 2 # E: Cannot assign to final attribute "x"
D.y = 2 # E: Cannot access final instance attribute "y" on class object \
# E: Cannot assign to final attribute "y"
[out]
[case testFinalReassignInstanceVarExternalInstance]
from typing import Final
class C:
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class D(C): pass
C().x = 2 # E: Cannot assign to final attribute "x"
D().x = 2 # E: Cannot assign to final attribute "x"
D().y = 2 # E: Cannot assign to final attribute "y"
[out]
[case testFinalWorksWithComplexTargets]
from typing import Final, Any
y: Final[Any] = 1
x = a, (b, y), c = 2, (2, 2), 2 # E: Cannot assign to final name "y"
t, *y, s = u = [2, 2, 2] # E: Cannot assign to final name "y"
[builtins fixtures/list.pyi]
[out]
[case testFinalInplaceAssign]
from typing import Final
class A: # no such things in fixtures
def __add__(self, other: A) -> A: ...
class B:
def __add__(self, other: B) -> B: ...
def __iadd__(self, other: B) -> B: ...
a: Final = A()
b: Final = B()
class C:
a: Final = A()
b: Final = B()
class D(C):
pass
a += A() # E: Cannot assign to final name "a"
b += B() # E: Cannot assign to final name "b"
D().a += A() # E: Cannot assign to final attribute "a"
D().b += B() # E: Cannot assign to final attribute "b"
[out]
-- Overriding
[case testFinalOverridingVarClassBody]
from typing import Final
# We use properties in this tests and below because we want to check
# that any existing variable before final doesn't affect logic of
# subsequent overrides but writable attributes cannot be overridden by final.
class A:
@property
def x(self) -> int: ...
@property
def y(self) -> int: ...
class B(A):
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class C(B):
x: int = 2 # E: Cannot assign to final name "x"
y: int = 2 # E: Cannot assign to final name "y"
x = 3 # E: Cannot assign to final name "x"
y = 3 # E: Cannot assign to final name "y"
class D(C):
pass
D.x = 4 # E: Cannot assign to final attribute "x"
D.y = 4 # E: Cannot assign to final attribute "y"
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarClassBodyExplicit]
from typing import Final
class A:
@property
def x(self) -> int: ...
@property
def y(self) -> int: ...
class B(A):
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class C(B):
x: Final = 2 # E: Cannot override final attribute "x" (previously declared in base class "B")
y: Final = 2 # E: Cannot override final attribute "y" (previously declared in base class "B")
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarInit]
from typing import Final
class A:
@property
def x(self) -> int: ...
@property
def y(self) -> int: ...
class B(A):
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class C(B):
def __init__(self) -> None:
self.x = 2 # E: Cannot assign to final attribute "x"
self.y = 2 # E: Cannot assign to final attribute "y"
def meth(self) -> None:
self.x = 3 # E: Cannot assign to final attribute "x"
self.y = 3 # E: Cannot assign to final attribute "y"
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarInit2]
from typing import Final
class A:
@property
def x(self) -> int: ...
@property
def y(self) -> int: ...
class B(A):
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class C(B):
def __init__(self) -> None:
self.x: Final = 2 # E: Cannot override final attribute "x" (previously declared in base class "B")
self.y: Final = 2 # E: Cannot override final attribute "y" (previously declared in base class "B")
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarOtherMethod]
from typing import Final
class A:
@property
def x(self) -> int: ...
@property
def y(self) -> int: ...
class B(A):
x: Final = 1
def __init__(self) -> None:
self.y: Final = 1
class C(B):
def meth(self) -> None:
self.x: int = 2 # E: Cannot assign to final attribute "x"
self.y: int = 2 # E: Cannot assign to final attribute "y"
self.x = 3 # E: Cannot assign to final attribute "x"
self.y = 3 # E: Cannot assign to final attribute "y"
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarMultipleInheritanceClass]
from typing import Final, Any
class A:
x: Final[Any] = 1
class B:
@property
def x(self) -> int: ...
class C(A, B): ...
class D(B, A): ... # E: Cannot override final attribute "x" (previously declared in base class "A")
C.x = 3 # E: Cannot assign to final attribute "x"
C().x = 4 # E: Cannot assign to final attribute "x"
D().x = 4 # E: Cannot assign to final attribute "x" \
# E: Property "x" defined in "B" is read-only
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarMultipleInheritanceInit]
from typing import Final, Any
class A:
def __init__(self) -> None:
self.x: Final[Any] = 1
class B:
@property
def x(self) -> int: ...
class C(A, B): ...
class D(B, A): ... # E: Cannot override final attribute "x" (previously declared in base class "A")
C.x = 3 # E: Cannot access final instance attribute "x" on class object \
# E: Cannot assign to final attribute "x"
C().x = 4 # E: Cannot assign to final attribute "x"
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarMultipleInheritanceMixed]
from typing import Final
class A:
x: Final = 1
class B:
def __init__(self) -> None:
self.x = 2
class C(A, B): ... # E: Cannot override writable attribute "x" with a final one
class D(B, A): ... # E: Cannot override final attribute "x" (previously declared in base class "A")
C.x = 3 # E: Cannot assign to final attribute "x"
D.x = 3 # E: Cannot assign to final attribute "x"
C().x = 4 # E: Cannot assign to final attribute "x"
D().x = 4 # E: Cannot assign to final attribute "x"
[out]
[case testFinalOverridingVarWithMethod]
from typing import Final, Any
class A:
x: Final[Any] = 1
def __init__(self) -> None:
self.y: Final[Any] = 1
class B(A):
def x(self) -> None: pass # E: Cannot override final attribute "x" (previously declared in base class "A")
def y(self) -> None: pass # E: Cannot override final attribute "y" (previously declared in base class "A")
class C(A):
@property # E: Cannot override final attribute "x" (previously declared in base class "A")
def x(self) -> None: pass
@property # E: Cannot override final attribute "y" (previously declared in base class "A")
def y(self) -> None: pass
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingVarWithMethodClass]
from typing import Final, Any
class A:
x: Final[Any] = 1
def __init__(self) -> None:
self.y: Final[Any] = 1
class B(A):
@classmethod # E: Cannot override final attribute "x" (previously declared in base class "A")
def x(self) -> None: pass
@classmethod # E: Cannot override final attribute "y" (previously declared in base class "A")
def y(self) -> None: pass
[builtins fixtures/classmethod.pyi]
[out]
[case testFinalOverridingMethodRegular]
from typing import final
class B:
@final
def meth(self) -> None: ...
class C(B):
def meth(self) -> None: ... # E: Cannot override final attribute "meth" (previously declared in base class "B")
[out]
[case testFinalOverridingMethodInitNew]
from typing import final
class B:
@final
def __init__(self) -> None: ...
@final
def __new__(cls) -> B: ...
class C(B):
def __init__(self) -> None: ... # E: Cannot override final attribute "__init__" (previously declared in base class "B")
def __new__(cls) -> B: ... # E: Cannot override final attribute "__new__" (previously declared in base class "B")
[out]
[case testFinalOverridingMethodWithVar]
from typing import final, Final, Any
a: Any
class A:
@final
def f(self) -> None: pass
@final
@property
def p(self) -> int: pass
class B(A):
f = a # E: Cannot override final attribute "f" (previously declared in base class "A")
p = a # E: Cannot override final attribute "p" (previously declared in base class "A")
class C(A):
f: Any # E: Cannot override final attribute "f" (previously declared in base class "A")
p: Any # E: Cannot override final attribute "p" (previously declared in base class "A")
class D(A):
f: Final = a # E: Cannot override final attribute "f" (previously declared in base class "A")
p: Final = a # E: Cannot override final attribute "p" (previously declared in base class "A")
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingMethodWithVarImplicit]
from typing import final, Any, Final
a: Any
class A:
@final
def f(self) -> None: pass
@final
@classmethod
def c(cls) -> int: pass
class B(A):
def __init__(self) -> None:
self.f: Any # E: Cannot assign to final attribute "f" \
# E: Cannot override final attribute "f" (previously declared in base class "A")
self.c: Any # E: Cannot assign to final attribute "c" \
# E: Cannot override final attribute "c" (previously declared in base class "A")
B().f = a # E: Cannot assign to final attribute "f"
B().c = a # E: Cannot assign to final attribute "c"
class C(A):
def __init__(self) -> None:
self.f: Final = a # E: Cannot override final attribute "f" (previously declared in base class "A")
self.c: Final = a # E: Cannot override final attribute "c" (previously declared in base class "A")
[builtins fixtures/classmethod.pyi]
[out]
[case testFinalCanOverrideMethodWithFinal]
from typing import final
class B:
def meth(self) -> None: ...
class C(B):
@final # OK
def meth(self) -> None: ...
[out]
[case testFinalOverridingMethodMultipleInheritance]
from typing import final
class A:
def m(self) -> int: pass
class B:
@final
def m(self) -> int: pass
class C(A, B): pass # E: Cannot override final attribute "m" (previously declared in base class "B")
class D(B, A): pass
[out]
[case testFinalOverridingMethodMultipleInheritanceVar]
from typing import final, Any
class A:
m: Any
class B:
@final
def m(self) -> int: pass
class C(A, B): pass # E: Cannot override final attribute "m" (previously declared in base class "B")
class D(B, A): pass # E: Cannot override writable attribute "m" with a final one
[out]
[case testFinalOverridingClassMethod]
from typing import final
class B:
@classmethod
@final
def f(cls) -> int: pass
class C(B):
@classmethod # E: Cannot override final attribute "f" (previously declared in base class "B")
def f(cls) -> int: pass
[builtins fixtures/classmethod.pyi]
[out]
[case testFinalOverridingStaticMethod]
from typing import final
class B:
@staticmethod
@final
def f() -> int: pass
@final
@staticmethod
def g() -> int: pass
class C(B):
@staticmethod # E: Cannot override final attribute "f" (previously declared in base class "B")
def f() -> int: pass
@staticmethod # E: Cannot override final attribute "g" (previously declared in base class "B")
def g() -> int: pass
[builtins fixtures/staticmethod.pyi]
[out]
[case testFinalOverridingProperty]
from typing import final
class B:
@final
@property
def f(self) -> int: pass
@property
@final
def g(self) -> int: pass
class C(B):
@property # E: Cannot override final attribute "f" (previously declared in base class "B")
def f(self) -> int: pass
@property # E: Cannot override final attribute "g" (previously declared in base class "B")
def g(self) -> int: pass
[builtins fixtures/property.pyi]
[out]
[case testFinalOverridingMethodOverloads]
from typing import final, overload
class B:
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
@final
def f(self, x):
pass
class C(B):
@overload # E: Cannot override final attribute "f" (previously declared in base class "B")
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
def f(self, x):
pass
[out]
[case testFinalClassNoInheritance]
from typing import final
@final
class B: ...
class C(B): # E: Cannot inherit from final class "B"
pass
class D(C): # E: Cannot inherit from final class "B"
pass
[out]
[case testFinalClassNoInheritanceMulti]
from typing import final
class A: ...
@final
class B: ...
class C(B, A): # E: Cannot inherit from final class "B"
pass
class D(A, B): # E: Cannot inherit from final class "B"
pass
[out]
[case testFinalCantOverrideWriteable]
from typing import Any, Final, final
class B:
x: Any
@property
def y(self) -> Any: ...
@y.setter
def y(self, x: Any) -> None: ...