diff --git a/mypy/subtypes.py b/mypy/subtypes.py index ca21e67f3a6e..a4d8618c8911 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -552,6 +552,10 @@ def visit_instance(self, left: Instance) -> bool: if left.type.alt_promote and left.type.alt_promote.type is right.type: return True rname = right.type.fullname + if left.type.fullname == "builtins.function" and rname == "types.FunctionType": + # Functions use builtins.function as their fallback, but this is + # the same runtime type as types.FunctionType. + return True # Always try a nominal check if possible, # there might be errors that a user wants to silence *once*. # NamedTuples are a special case, because `NamedTuple` is not listed diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 321a0afebd5a..0ec7056583c6 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -283,6 +283,66 @@ if int(): class A: pass [builtins fixtures/tuple.pyi] +[case testTypesFunctionTypeDecorator] +from types import FunctionType + +def dec(func: FunctionType) -> FunctionType: + return func + +@dec +def f() -> None: + pass + +reveal_type(f) # N: Revealed type is "types.FunctionType" +f() +dec(1) # E: Argument 1 to "dec" has incompatible type "int"; expected "FunctionType" +dec(object()) # E: Argument 1 to "dec" has incompatible type "object"; expected "FunctionType" +[file types.pyi] +from typing import Any +class FunctionType: + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... +[builtins fixtures/dict.pyi] + +[case testTypesFunctionTypeSubtyping] +from types import FunctionType +from typing import Callable, overload + +def f() -> None: pass + +func: FunctionType = f +func = lambda: None +callback: Callable[[int], str] +func = callback + +@overload +def overloaded(x: int) -> int: ... +@overload +def overloaded(x: str) -> str: ... +def overloaded(x: int | str) -> int | str: + return x + +func = overloaded + +class CallableClass: + def __call__(self) -> None: pass + +func = CallableClass() # E: Incompatible types in assignment (expression has type "CallableClass", variable has type "FunctionType") +func = CallableClass # E: Incompatible types in assignment (expression has type "type[CallableClass]", variable has type "FunctionType") + +class OverloadedClass: + @overload + def __init__(self, x: int) -> None: ... + @overload + def __init__(self, x: str) -> None: ... + def __init__(self, x: int | str) -> None: pass + +func = OverloadedClass # E: Incompatible types in assignment (expression has type "type[OverloadedClass]", variable has type "FunctionType") +[file types.pyi] +from typing import Any +class FunctionType: + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... +[builtins fixtures/dict.pyi] + [case testReturnEmptyTuple] from typing import Tuple def f(x): # type: (int) -> () # E: Syntax error in type annotation \