Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Loading