@@ -2725,3 +2725,154 @@ TS = TypeVar("TS", bound=str)
2725
2725
f: Callable[[Sequence[TI]], None]
2726
2726
g: Callable[[Union[Sequence[TI], Sequence[TS]]], None]
2727
2727
f = g
2728
+
2729
+ [case explicitOverride]
2730
+ from typing import override
2731
+
2732
+ class A:
2733
+ def f(self, x: int) -> str: pass
2734
+ @override
2735
+ def g(self, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2736
+
2737
+ class B(A):
2738
+ @override
2739
+ def f(self, x: int) -> str: pass
2740
+ @override
2741
+ def g(self, x: int) -> str: pass
2742
+
2743
+ class C(A):
2744
+ @override
2745
+ def f(self, x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \
2746
+ # N: This violates the Liskov substitution principle \
2747
+ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
2748
+ def g(self, x: int) -> str: pass
2749
+
2750
+ class D(A): pass
2751
+ class E(D): pass
2752
+ class F(E):
2753
+ @override
2754
+ def f(self, x: int) -> str: pass
2755
+ [typing fixtures/typing-medium.pyi]
2756
+
2757
+ [case explicitOverrideStaticmethod]
2758
+ from typing import override
2759
+
2760
+ class A:
2761
+ @staticmethod
2762
+ def f(x: int) -> str: pass
2763
+
2764
+ class B(A):
2765
+ @staticmethod
2766
+ @override
2767
+ def f(x: int) -> str: pass
2768
+ @override
2769
+ @staticmethod
2770
+ def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2771
+
2772
+ class C(A): # inverted order of decorators
2773
+ @override
2774
+ @staticmethod
2775
+ def f(x: int) -> str: pass
2776
+ @override
2777
+ @staticmethod
2778
+ def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2779
+
2780
+ class D(A):
2781
+ @staticmethod
2782
+ @override
2783
+ def f(x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \
2784
+ # N: This violates the Liskov substitution principle \
2785
+ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
2786
+ [typing fixtures/typing-medium.pyi]
2787
+ [builtins fixtures/callable.pyi]
2788
+
2789
+ [case explicitOverrideClassmethod]
2790
+ from typing import override
2791
+
2792
+ class A:
2793
+ @classmethod
2794
+ def f(cls, x: int) -> str: pass
2795
+
2796
+ class B(A):
2797
+ @classmethod
2798
+ @override
2799
+ def f(cls, x: int) -> str: pass
2800
+ @override
2801
+ @classmethod
2802
+ def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2803
+
2804
+ class C(A): # inverted order of decorators
2805
+ @override
2806
+ @classmethod
2807
+ def f(cls, x: int) -> str: pass
2808
+ @override
2809
+ @classmethod
2810
+ def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2811
+
2812
+ class D(A):
2813
+ @classmethod
2814
+ @override
2815
+ def f(cls, x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \
2816
+ # N: This violates the Liskov substitution principle \
2817
+ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
2818
+ [typing fixtures/typing-medium.pyi]
2819
+ [builtins fixtures/callable.pyi]
2820
+
2821
+ [case explicitOverrideProperty]
2822
+ from typing import override
2823
+
2824
+ class A:
2825
+ @property
2826
+ def f(self) -> str: pass
2827
+
2828
+ class B(A):
2829
+ @property
2830
+ @override
2831
+ def f(self) -> str: pass
2832
+ @override
2833
+ @property
2834
+ def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2835
+
2836
+ class C(A): # inverted order of decorators
2837
+ @override
2838
+ @property
2839
+ def f(self) -> str: pass
2840
+ @override
2841
+ @property
2842
+ def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
2843
+
2844
+ class D(A):
2845
+ @property
2846
+ @override
2847
+ def f(self) -> int: pass # E: Signature of "f" incompatible with supertype "A"
2848
+ [builtins fixtures/property.pyi]
2849
+ [typing fixtures/typing-medium.pyi]
2850
+
2851
+ [case invalidExplicitOverride]
2852
+ from typing import override
2853
+
2854
+ @override # E: "override" used with a non-method
2855
+ def f(x: int) -> str: pass
2856
+
2857
+ @override # this should probably throw an error but the signature from typeshed should ensure this already
2858
+ class A: pass
2859
+
2860
+ def g() -> None:
2861
+ @override # E: "override" used with a non-method
2862
+ def h(b: bool) -> int: pass
2863
+ [typing fixtures/typing-medium.pyi]
2864
+
2865
+ [case explicitOverrideSpecialMethods]
2866
+ from typing import override
2867
+
2868
+ class A:
2869
+ def __init__(self, a: int) -> None: pass
2870
+
2871
+ class B(A):
2872
+ @override
2873
+ def __init__(self, b: str) -> None: pass
2874
+
2875
+ class C:
2876
+ @override
2877
+ def __init__(self, a: int) -> None: pass
2878
+ [typing fixtures/typing-medium.pyi]
0 commit comments