Skip to content

partial plugin does not handle overloads #17585

Open

Description

@asottile-sentry

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

Activity

  1. asottile-sentry commented on Jul 25, 2024

    @asottile-sentry
    Author

    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)
  2. hauntsaninja commented on Jul 26, 2024

    @hauntsaninja
    Collaborator

    Yes, the behaviour with overloads is unchanged from mypy 1.10. Shouldn't be too bad to add support for it.

    # TODO: handle overloads, just fall back to whatever the non-plugin code does
    return ctx.default_return_type

    The constrained type var example you have is a duplicate of #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.

  3. AaronDMarasco commented on Dec 10, 2025

    @AaronDMarasco

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions