`partial` plugin does not handle overloads
Bug Report
it appears the new partial plugin added in mypy 1.11 doesn't properly handle overloads (and just picks the first one)
To Reproduce
import functools
from typing import assert_type, overload
@overload
def identity(x: int, *, p: str) -> int: ...
@overload
def identity(x: str, *, p: str) -> str: ...
def identity(x: int | str, *, p: str) -> int | str: return x
p = functools.partial(identity, p='hi')
assert_type(p(1), int)
assert_type(p('hi'), str)
Expected Behavior
(no errors)
Actual Behavior
$ mypy t.py
t.py:13: error: Expression is of type "int", not "str" [assert-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.11.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.2
an alternative spelling with TypeVars seems to not work either:
import functools
from typing import assert_type, overload, TypeVar
T = TypeVar("T", int, str)
def identity(x: T, *, p: str) -> T: return x
p = functools.partial(identity, p='hi')
assert_type(p(1), int)
assert_type(p('hi'), str)
$ ./venv/bin/mypy t.py
t.py:10: error: Expression is of type "Any", not "int" [assert-type]
t.py:11: error: Expression is of type "Any", not "str" [assert-type]
Found 2 errors in 1 file (checked 1 source file)
Yes, the behaviour with overloads is unchanged from mypy 1.10. Shouldn't be too bad to add support for it. https://github.com/python/mypy/blob/d1d3c780c7a2d30b2a038903289ea7487303a218/mypy/plugins/functools.py#L131-L132
The constrained type var example you have is a duplicate of https://github.com/python/mypy/issues/17411. Note that unconstrained type vars or bounded type vars should work correctly. In general, mypy's handling of constrained type vars is a little weird.
I think I'm hitting this as well in mypy 1.18.2. I won't post the actual code, but the gist is that the overload should be chosen based on an enum choice:
- Have an
enum"CommandsEnum" that describes a set of functions a functioncommands_corecan perform - Have
@overloads of that function with thecommandset to something likeLiteral[CommandsEnum.Command3] - Create a partial with
do_comm3 = partial(commands_core, command=CommandsEnum.Command3)
It seems to always pick the "first" overload.