Skip to content

Spurious error with partial and TypeVar #18953

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

Open
mharding-hpe opened this issue Apr 22, 2025 · 0 comments · May be fixed by #18954
Open

Spurious error with partial and TypeVar #18953

mharding-hpe opened this issue Apr 22, 2025 · 0 comments · May be fixed by #18954
Labels
bug mypy got something wrong

Comments

@mharding-hpe
Copy link

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
@mharding-hpe mharding-hpe added the bug mypy got something wrong label Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant