Skip to content

Odd error message for invalid use of tuple(): expected "Iterable[Never]" #19098

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
wyattscarpenter opened this issue May 16, 2025 · 7 comments
Labels
bug mypy got something wrong topic-error-reporting How we report errors

Comments

@wyattscarpenter
Copy link
Contributor

tuple(1) is invalid, of course. But what error message does it bring?

main.py:1: error: Argument 1 to "tuple" has incompatible type "int"; expected "Iterable[Never]"  [arg-type]

Well that's odd! Considering that it can take iterables of all sorts of things, and not... impossible things.

In fact, this has to be wrong, because when I make my own function with Iterable[Never] argument, it doesn't like taking iterables of other things

from typing import Iterable, Never

def f(a: Iterable[Never]) -> None:
    pass

f([1,2,3]) # main.py:6: error: Argument 1 to "f" has incompatible type "list[int]"; expected "Iterable[Never]"  [arg-type]
@wyattscarpenter wyattscarpenter added the bug mypy got something wrong label May 16, 2025
@sterliakov
Copy link
Collaborator

Tuple constructor takes Iterable[T] where T is a type variable. Mypy cannot infer any value of T such that int is a subtype of Iterable[T] and so has to say Never - that's what we do with all unsolvable type variables. I agree this is questionable from usability standpoint.

@wyattscarpenter
Copy link
Contributor Author

Ah, yes, it's the same for any user-level function as well...

from typing import Iterable, Never

def f[T](a: Iterable[T]) -> None:
    pass

f(1) # main.py:6: error: Argument 1 to "f" has incompatible type "int"; expected "Iterable[Never]"  [arg-type]

@wyattscarpenter
Copy link
Contributor Author

It's hard to say what the type checker could usefully and accurately say here. Never is too narrow, Any is too broad, int (the actual value of T in this case) is also unhelpful). I guess the best option might be just printing the name of the typevar instead.

@sterliakov
Copy link
Collaborator

But typevar name is also weird, some people use long-ish typevar names, and that can look like some "good" type one has to look up. Even short names like T won't really friendly for beginners who aren't familiar with typevars (or at least not proficient with typing to expect them in such contexts).

@sterliakov sterliakov added the topic-error-reporting How we report errors label May 17, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented May 17, 2025

We used to use Unknown <nothing> in the error message IIRC and maybe that's cleaner? Alternatively maybe we can also emit a "unable to infer type vars" just like with variable assignment if a function call fails?

(I think any sort of proper solution for this is really hard, I would love an interface to explore a program and find what constraints are used for inference etc, but that's a massive undertaking...)

@wyattscarpenter
Copy link
Contributor Author

Maybe instead of <nothing>, it should print <something>. Probably kind of hard to get it to know the difference between Nevers that are something not given and Nevers that are real Nevers, though...

@A5rocks
Copy link
Collaborator

A5rocks commented May 17, 2025

It's very easy, the Never is explicitly assigned upon not finding anything to infer. We would just need to add some sort of "kind" parameter like Any I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-error-reporting How we report errors
Projects
None yet
Development

No branches or pull requests

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy