Skip to content
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

Literal[inf] / Literal[nan]: misleading error message #11208

Open
crusaderky opened this issue Sep 27, 2021 · 1 comment
Open

Literal[inf] / Literal[nan]: misleading error message #11208

crusaderky opened this issue Sep 27, 2021 · 1 comment
Labels
bug mypy got something wrong diagnostics topic-error-reporting How we report errors

Comments

@crusaderky
Copy link
Contributor

crusaderky commented Sep 27, 2021

from math import inf, nan
from typing import Literal

x: Literal[1.1]
y: Literal[inf]
z: Literal[nan]

mypy 0.910 output:

a.py:4: error: Parameter 1 of Literal[...] cannot be of type "float"
a.py:5: error: Parameter 1 of Literal[...] is invalid
a.py:5: error: Variable "math.inf" is not valid as a type
a.py:5: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
a.py:6: error: Parameter 1 of Literal[...] is invalid
a.py:6: error: Variable "math.nan" is not valid as a type
a.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

The linked documentation is unrelated to the issue, as Literal does not expect types.
Expected output:

a.py:4: error: Parameter 1 of Literal[...] cannot be of type "float"
a.py:5: error: Parameter 1 of Literal[...] cannot be of type "float"
a.py:6: error: Parameter 1 of Literal[...] cannot be of type "float"
@crusaderky crusaderky added the bug mypy got something wrong label Sep 27, 2021
@hauntsaninja
Copy link
Collaborator

Not sure about this one. It's perfectly valid to use type aliases in Literals (e.g. https://www.python.org/dev/peps/pep-0586/#legal-parameters-for-literal-at-type-check-time ) and the note seems about as valid as any other place where a use wanted to put a type alias but ended up putting a variable.

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

No branches or pull requests

3 participants