Skip to content

Spurious error with partial and TypeVar #18953

Closed
@mharding-hpe

Description

@mharding-hpe

Bug Report

mypy reports a (false) incompatible type error when partial is used on functions with TypeVars.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&flags=verbose&gist=b65b8dade3556b815d146c8e8ff95427

from typing import TypeVar, overload, Callable
from functools import partial

def harf(x: Callable[[int], int]) -> None:
    pass

def func1(a: int, b: str="truck") -> int:
    return a

def func2[I: (int, str)](a: I, *,  b: str="truck") -> I:
    return a

T = TypeVar("T", int, str)

def func2b(a: T, *,  b: str="truck") -> T:
    return a

@overload
def func3(a: int, b:str=...) -> int: ...

@overload
def func3(a: str, b:str=...) -> str: ...

def func3(a: int | str, b: str = "trucl") -> int | str:
    return a

harf(func1)
harf(func2)
harf(func2b)
harf(func3)
harf(partial(func1, b="harf"))
harf(partial(func2, b="harf"))
harf(partial(func2b, b="harf"))
harf(partial(func3, b="harf"))

Expected Behavior

I do not expect any errors to be reported.

Actual Behavior

main.py:32: error: Argument 1 to "harf" has incompatible type "partial[I]"; expected "Callable[[int], int]"  [arg-type]
main.py:32: note: "partial[I].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], I]"
main.py:33: error: Argument 1 to "harf" has incompatible type "partial[T]"; expected "Callable[[int], int]"  [arg-type]
main.py:33: note: "partial[T].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], T]"

Those errors are for the two lines where partial is used on func2 and func2b, the ones which use a TypeVar. No error is reported when used with func3, which I believe is an equivalent function, only using overload instead of a TypeVar.

This looks like it may be related to #15215

Your Environment

  • Mypy version used: 1.15
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions