Skip to content

Show name of type variable in "Cannot infer type argument" message #19290

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

Merged

Conversation

brianschubert
Copy link
Collaborator

@brianschubert brianschubert commented Jun 13, 2025

Fixes #19289

The type argument index currently shown can be wrong if other type arguments were substituted during an earlier pass.

Given:

def foo[T1, T2](
    a: T1,
    b: T2,
    c: Callable[[T2], T2],
) -> tuple[T1, T2]: ...

def bar(y: float) -> float: ...

reveal_type(foo(1, None, bar))  # Expect T1=int, T2=<failed>

Before:

main.py:9: error: Cannot infer type argument 1 of "foo"  [misc]
main.py:9: note: Revealed type is "tuple[builtins.int, Any]"

After:

main.py:9: error: Cannot infer type argument to type parameter "T2" of "foo"  [misc]
main.py:9: note: Revealed type is "tuple[builtins.int, Any]"

@brianschubert
Copy link
Collaborator Author

brianschubert commented Jun 13, 2025

primer shard 0 died due to hitting the 60 minute timeout :(

This comment has been minimized.

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG! Only a minor optional nit:

This comment has been minimized.

@brianschubert
Copy link
Collaborator Author

I'm a little confused how a new test case in master is leaking into this PR's CI: https://github.com/python/mypy/actions/runs/15734614442/job/44343883908?pr=19290#step:9:210

The failing case was added in #19279. That commit isn't in this branch's history and the failing case does not exist locally.

Trivial to fix, but still weird.

mypy/messages.py Outdated
self.fail(f"Cannot infer type argument {n} of {callee_name}", context)
if callee_name is not None:
self.fail(
f"Cannot infer type argument to type parameter {format_type(tv, self.options)} of {callee_name}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"type argument to type parameter" feels unnecessarily verbose. I think this is clear enough:

Suggested change
f"Cannot infer type argument to type parameter {format_type(tv, self.options)} of {callee_name}",
f"Cannot infer type argument {format_type(tv, self.options)} of {callee_name}",

Copy link
Collaborator Author

@brianschubert brianschubert Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree with this being verbose. Cannot infer type argument "T" originally sounded off to me since T isn't a type argument, it's the type parameter whose argument mypy is trying to infer.

Maybe Cannot infer value of type parameter "T" of "f"? I'm also fine with the suggested wording if that's your preference.

@sterliakov
Copy link
Collaborator

sterliakov commented Jun 18, 2025

how a new test case in master is leaking into this PR's CI

actions/checkout checks out the merge base, not the PR HEAD. That's why actions do not run if PR has merge conflicts, for example. So tests are never run against your head revision, but rather against the result of merging your branch into the destination branch as of now.

@brianschubert
Copy link
Collaborator Author

TIL, thank you!

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/task_engine.py:648: error: Cannot infer type argument 2 of "update_for_task" of "ResultStore"  [misc]
+ src/prefect/task_engine.py:648: error: Cannot infer value of type parameter "R" of "update_for_task" of "ResultStore"  [misc]
- src/prefect/task_engine.py:1233: error: Cannot infer type argument 2 of "update_for_task" of "ResultStore"  [misc]
+ src/prefect/task_engine.py:1233: error: Cannot infer value of type parameter "R" of "update_for_task" of "ResultStore"  [misc]

werkzeug (https://github.com/pallets/werkzeug)
- src/werkzeug/datastructures/structures.py:711: error: Cannot infer type argument 2 of "setdefault" of "MutableMapping"  [misc]
+ src/werkzeug/datastructures/structures.py:711: error: Cannot infer value of type parameter "_VT" of "setdefault" of "MutableMapping"  [misc]

@hauntsaninja hauntsaninja merged commit a48ffed into python:master Jun 18, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mypy reports error for different argument, than the one which is actually wrong
4 participants