Repository navigation
Allow functions where types.FunctionType is expected (#22502) - #22503
Closed
zhengdaodeguL wants to merge 1 commit into
Closed
zhengdaodeguL wants to merge 1 commit into
zhengdaodeguL wants to merge 1 commit into
Conversation
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.
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]
|
|
New error in starlette, because if variable has type is from types import FunctionType
from typing import Callable
def f(x: FunctionType) -> None:
pass
def g() -> Callable:
return f
f(g())Expected Behavior Actual Behavior |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22502.
Decorating a function with a decorator typed as
types.FunctionTypewas rejected:while pyright, ty and pyrefly all accept it. The root cause is that mypy gives functions
builtins.functionas their fallback, but that is the same runtime type astypes.FunctionType. The subtype check inmypy/subtypes.pynow treatsbuiltins.functionas a subtype oftypes.FunctionType.Tests: added
testTypesFunctionTypeDecorator(the reported repro, plus negative cases forint/object) andtestTypesFunctionTypeSubtyping(assignment from functions, lambdas,Callables and overloads;CallableClassinstances and classes are still rejected) intest-data/unit/check-functions.test. Both new tests pass; the repro now reports no issues and the negative cases still error.