Skip to content

Commit 1042e69

Browse files
committed
Allow @OverRide on classes with Any fallback
1 parent 1725a12 commit 1042e69

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

mypy/checker.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
750750
defn.is_explicit_override
751751
and not found_method_base_classes
752752
and found_method_base_classes is not None
753+
# If the class has Any fallback, we can't be certain that a method
754+
# is really missing - it might come from unfollowed import.
755+
and not defn.info.fallback_to_any
753756
):
754757
self.msg.no_overridable_method(defn.name, defn)
755758
self.check_explicit_override_decorator(defn, found_method_base_classes, defn.impl)
@@ -5291,6 +5294,9 @@ def visit_decorator_inner(
52915294
e.func.is_explicit_override
52925295
and not found_method_base_classes
52935296
and found_method_base_classes is not None
5297+
# If the class has Any fallback, we can't be certain that a method
5298+
# is really missing - it might come from unfollowed import.
5299+
and not e.func.info.fallback_to_any
52945300
):
52955301
self.msg.no_overridable_method(e.func.name, e.func)
52965302
self.check_explicit_override_decorator(e.func, found_method_base_classes)

test-data/unit/check-functions.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,6 +3294,28 @@ class Parent: pass
32943294
class Child(Parent):
32953295
@override
32963296
def foo(self, y): pass # E: Method "foo" is marked as an override, but no base method was found with this name
3297+
3298+
[typing fixtures/typing-override.pyi]
3299+
3300+
[case testOverrideOnUnknownBaseClass]
3301+
# flags: --python-version 3.12
3302+
from typing import overload, override
3303+
3304+
from unknown import UnknownParent # type: ignore[import-not-found]
3305+
3306+
class UnknownChild(UnknownParent):
3307+
@override
3308+
def foo(self, y): pass # OK
3309+
@override
3310+
def bar(self, y: str) -> None: pass # OK
3311+
3312+
@override
3313+
@overload
3314+
def baz(self, y: str) -> None: ...
3315+
@override
3316+
@overload
3317+
def baz(self, y: int) -> None: ...
3318+
def baz(self, y: str | int) -> None: ...
32973319
[typing fixtures/typing-override.pyi]
32983320

32993321
[case testCallableProperty]

0 commit comments

Comments
 (0)