From c93c3dfadb2bface82c415e238ec69a87bcc5c4e Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Thu, 8 Apr 2021 23:11:02 +0200 Subject: [PATCH 1/2] Issue an error when overriding typeguard with non-typeguard in subclass --- mypy/checker.py | 3 +++ test-data/unit/check-typeguard.test | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index fce7e7d7a08e..50b503989800 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1610,6 +1610,9 @@ def check_override(self, override: FunctionLike, original: FunctionLike, if isinstance(original, FunctionLike) and isinstance(override, FunctionLike): if original_class_or_static and not override_class_or_static: fail = True + if isinstance(original, CallableType) and isinstance(override, CallableType): + if original.type_guard is not None and override.type_guard is None: + fail = True if is_private(name): fail = False diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index e4bf3dd5c931..01a8dfc9e69b 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -287,10 +287,10 @@ def main(a: object) -> None: reveal_type(a) # N: Revealed type is 'builtins.float' [builtins fixtures/tuple.pyi] -[case testTypeGuardMethodOverride-skip] +[case testTypeGuardMethodOverride] from typing_extensions import TypeGuard class C: def is_float(self, a: object) -> TypeGuard[float]: pass class D(C): - def is_float(self, a: object) -> bool: pass # E: Some error + def is_float(self, a: object) -> bool: pass # E: Signature of "is_float" incompatible with supertype "C" [builtins fixtures/tuple.pyi] From d7a911596a52c4d893ed4ce14c6d0ea55eaa8f71 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Thu, 8 Apr 2021 23:15:41 +0200 Subject: [PATCH 2/2] Use elif to avoid redundant check --- mypy/checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 50b503989800..704bf34e2db2 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1610,7 +1610,7 @@ def check_override(self, override: FunctionLike, original: FunctionLike, if isinstance(original, FunctionLike) and isinstance(override, FunctionLike): if original_class_or_static and not override_class_or_static: fail = True - if isinstance(original, CallableType) and isinstance(override, CallableType): + elif isinstance(original, CallableType) and isinstance(override, CallableType): if original.type_guard is not None and override.type_guard is None: fail = True