Skip to content

Allow functions where types.FunctionType is expected (#22502) - #22503

Closed
zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/functiontype-decorator-22502
Closed

zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/functiontype-decorator-22502

Conversation

@zhengdaodeguL

Copy link
Copy Markdown

Fixes #22502.

Decorating a function with a decorator typed as types.FunctionType was rejected:

error: Argument 1 to "dec" has incompatible type "Callable[[], None]"; expected "FunctionType"  [arg-type]

while pyright, ty and pyrefly all accept it. The root cause is that mypy gives functions builtins.function as their fallback, but that is the same runtime type as types.FunctionType. The subtype check in mypy/subtypes.py now treats builtins.function as a subtype of types.FunctionType.

Tests: added testTypesFunctionTypeDecorator (the reported repro, plus negative cases for int/object) and testTypesFunctionTypeSubtyping (assignment from functions, lambdas, Callables and overloads; CallableClass instances and classes are still rejected) in test-data/unit/check-functions.test. Both new tests pass; the repro now reports no issues and the negative cases still error.

Functions use builtins.function as their fallback, but that is the same
runtime type as types.FunctionType, so a callable should be assignable
to it. Teach the subtype check that builtins.function is a subtype of
types.FunctionType. Previously decorating a function with a decorator
typed as FunctionType was rejected with an arg-type error, while
pyright, ty and pyrefly accept it.

Adds testTypesFunctionTypeDecorator (the reported repro, plus negative
cases) and testTypesFunctionTypeSubtyping.
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

strawberry (https://github.com/strawberry-graphql/strawberry)
+ strawberry/pydantic/resolver_field.py:134: error: Unused "type: ignore" comment  [unused-ignore]

egglog-python (https://github.com/egraphs-good/egglog-python)
+ python/egglog/egraph.py:1759: error: Redundant cast to "Action | BaseExpr | Fact | RewriteOrRule"  [redundant-cast]

pydantic (https://github.com/pydantic/pydantic)
+ pydantic/validate_call_decorator.py:116: error: Unused "type: ignore" comment  [unused-ignore]
- pydantic/experimental/arguments_schema.py:47: error: Argument 1 to "_arguments_schema" of "GenerateSchema" has incompatible type "Callable[..., Any]"; expected "FunctionType | MethodType | partial[Any]"  [arg-type]
- pydantic/experimental/arguments_schema.py:49: error: Argument 1 to "_arguments_v3_schema" of "GenerateSchema" has incompatible type "Callable[..., Any]"; expected "FunctionType | MethodType | partial[Any]"  [arg-type]

pytest-autoprofile (https://gitlab.com/TTsangSC/pytest-autoprofile)
+ src/pytest_autoprofile/profiler.py:560: error: Unused "type: ignore" comment  [unused-ignore]
+ src/pytest_autoprofile/profiler.py:602: error: Overloaded function signature 10 will never be matched: signature 3's parameter type(s) are the same or broader  [overload-cannot-match]
+ src/pytest_autoprofile/profiler.py:646: error: Unused "type: ignore" comment  [unused-ignore]
+ src/pytest_autoprofile/profiler.py:688: error: Overloaded function signature 10 will never be matched: signature 3's parameter type(s) are the same or broader  [overload-cannot-match]

beartype (https://github.com/beartype/beartype)
+ beartype/_util/cache/utilcacheobjattr.py:584: error: Unused "type: ignore" comment  [unused-ignore]

pytest (https://github.com/pytest-dev/pytest)
+ testing/test_warning_types.py:48: error: Unused "type: ignore" comment  [unused-ignore]

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/profiling/_asyncio.py:452: error: Unused "type: ignore" comment  [unused-ignore]
+ ddtrace/internal/utils/inspection.py:83: error: Redundant cast to "FunctionType"  [redundant-cast]
- ddtrace/contrib/internal/anyio/patch.py:66: error: Argument 1 to "wrap" has incompatible type "def [PosArgsT, T_Retval] run_sync(func: def (*args: *PosArgsT) -> T_Retval, *args: *PosArgsT, abandon_on_cancel: bool = ..., cancellable: bool | None = ..., limiter: CapacityLimiter | None = ...) -> Coroutine[Any, Any, T_Retval]"; expected "FunctionType"  [arg-type]
+ ddtrace/internal/symbol_db/symbols.py:534: error: Redundant cast to "FunctionType"  [redundant-cast]
+ ddtrace/contrib/internal/asyncio/_context_switch.py:50: error: Unused "type: ignore" comment  [unused-ignore]
+ ddtrace/contrib/internal/asyncio/_context_switch.py:52: error: Unused "type: ignore" comment  [unused-ignore]
+ ddtrace/contrib/internal/asyncio/_context_switch.py:57: error: Unused "type: ignore" comment  [unused-ignore]
- ddtrace/contrib/internal/asyncio/_context_switch.py:56: error: Argument 1 to "wrap" has incompatible type "def [_T_co] eager_task_factory(loop: AbstractEventLoop | None, coro: Coroutine[Any, Any, _T_co], *, name: str | None = ..., context: Context | None = ..., eager_start: bool = ...) -> Task[_T_co]"; expected "FunctionType"  [arg-type]
- ddtrace/contrib/internal/asyncio/_context_switch.py:56: note: Error code "arg-type" not covered by "type: ignore[attr-defined]" comment
+ ddtrace/debugging/_origin/span.py:192: error: Redundant cast to "FunctionType"  [redundant-cast]

starlette (https://github.com/encode/starlette)
+ starlette/routing.py:245: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:246: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:247: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:263: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:281: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:282: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:296: error: Cannot determine type of "methods"  [has-type]
+ starlette/routing.py:301: error: Cannot determine type of "methods"  [has-type]
+ starlette/schemas.py:80: error: Cannot determine type of "methods"  [has-type]

@zedzhen

zedzhen commented Nov 17, 2026

Copy link
Copy Markdown

New error in starlette, because if variable has type is Callable[..., Any], inspect.isfunction(callable) always evaluated by mypy as True.

from types import FunctionType
from typing import Callable


def f(x: FunctionType) -> None:
    pass


def g() -> Callable:
    return f


f(g())

Expected Behavior
(as in the master branch/version 2.4.1)
error: Argument 1 to "f" has incompatible type "Callable[..., Any]"; expected "FunctionType" [arg-type]

Actual Behavior
no error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't get FunctionType

3 participants