mypy icon indicating copy to clipboard operation
mypy copied to clipboard

`partial` plugin does not handle overloads

Open asottile-sentry opened this issue 1 year ago • 3 comments

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

asottile-sentry avatar Jul 25 '24 16:07 asottile-sentry

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)

asottile-sentry avatar Jul 25 '24 16:07 asottile-sentry

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.

hauntsaninja avatar Jul 26 '24 07:07 hauntsaninja

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 function commands_core can perform
  • Have @overloads of that function with the command set to something like Literal[CommandsEnum.Command3]
  • Create a partial with do_comm3 = partial(commands_core, command=CommandsEnum.Command3)

It seems to always pick the "first" overload.

AaronDMarasco avatar Dec 10 '25 15:12 AaronDMarasco