Skip to content

Fix functools.partial with Union of Any #17283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
6 changes: 4 additions & 2 deletions mypy/plugins/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mypy.checker
import mypy.plugin
from mypy.argmap import map_actuals_to_formals
from mypy.expandtype import expand_type
from mypy.nodes import ARG_POS, ARG_STAR2, ArgKind, Argument, FuncItem, Var
from mypy.plugins.common import add_method_to_class
from mypy.types import (
Expand Down Expand Up @@ -124,11 +125,12 @@ def partial_new_callback(ctx: mypy.plugin.FunctionContext) -> Type:
return ctx.default_return_type
if len(ctx.arg_types[0]) != 1:
return ctx.default_return_type
fn = expand_type(get_proper_type(ctx.arg_types[0][0]), {})

if isinstance(get_proper_type(ctx.arg_types[0][0]), Overloaded):
if isinstance(fn, Overloaded):
# TODO: handle overloads, just fall back to whatever the non-plugin code does
return ctx.default_return_type
fn_type = ctx.api.extract_callable_type(ctx.arg_types[0][0], ctx=ctx.default_return_type)
fn_type = ctx.api.extract_callable_type(fn, ctx=ctx.default_return_type)
if fn_type is None:
return ctx.default_return_type

Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-functools.test
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,13 @@ p(bar, 1, "a", 3.0) # OK
p(bar, 1, "a", 3.0, kwarg="asdf") # OK
p(bar, 1, "a", "b") # E: Argument 1 to "foo" has incompatible type "Callable[[int, str, float], None]"; expected "Callable[[int, str, str], None]"
[builtins fixtures/dict.pyi]

[case testFunctoolsPartialUnion]
import functools
from typing import Any, Union

cls1: Any
cls2: Union[Any, Any]
functools.partial(cls1, 2)
functools.partial(cls2, 2)
[builtins fixtures/tuple.pyi]
Loading