diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 36cd0a213d4d..6c8d03319893 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -568,6 +568,13 @@ def verify_typeinfo( # Catch all exceptions in case the runtime raises an unexpected exception # from __getattr__ or similar. continue + + # If it came from the metaclass, consider the runtime_attr to be MISSING + # for a more accurate message + if runtime_attr is not MISSING and type(runtime) is not runtime: + if getattr(runtime_attr, "__objclass__", None) is type(runtime): + runtime_attr = MISSING + # Do not error for an object missing from the stub # If the runtime object is a types.WrapperDescriptorType object # and has a non-special dunder name. @@ -1519,6 +1526,7 @@ def is_probably_a_function(runtime: Any) -> bool: isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType)) or isinstance(runtime, (types.MethodType, types.BuiltinMethodType)) or (inspect.ismethoddescriptor(runtime) and callable(runtime)) + or (isinstance(runtime, types.MethodWrapperType) and callable(runtime)) ) diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index fcbf07b4d371..b16cb18ace21 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -1460,6 +1460,16 @@ def h(x: str): ... runtime="__all__ += ['Z']\nclass Z:\n def __reduce__(self): return (Z,)", error=None, ) + # __call__ exists on type, so it appears to exist on the class. + # This checks that we identify it as missing at runtime anyway. + yield Case( + stub=""" + class ClassWithMetaclassOverride: + def __call__(*args, **kwds): ... + """, + runtime="class ClassWithMetaclassOverride: ...", + error="ClassWithMetaclassOverride.__call__", + ) @collect_cases def test_missing_no_runtime_all(self) -> Iterator[Case]: