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

Infer Literal type for + on literal #16728

Closed
JelleZijlstra opened this issue Jan 2, 2024 · 0 comments · Fixed by #16910
Closed

Infer Literal type for + on literal #16728

JelleZijlstra opened this issue Jan 2, 2024 · 0 comments · Fixed by #16910

Comments

@JelleZijlstra
Copy link
Member

Currently this fails:

from typing import Literal

x: Literal[1] = +1

main.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "Literal[1]") [assignment]

https://mypy-play.net/?mypy=latest&python=3.10&gist=ca355823e5fdc571fb3efa9c4f0bb764

Pyright allows it; mypy probably should too.

This is similar to python/typing#1550, but technically independent as the proposed spec change is about what goes inside Literal[], and this issue is about contexts where mypy should infer literal types, which is not specified precisely.

JelleZijlstra pushed a commit that referenced this issue Feb 12, 2024
This makes unary positive on integers preserve the literal value of the
integer, allowing `var: Literal[1] = +1` to be accepted. Basically I
looked for code handling `__neg__` and added a branch for `__pos__` as
well.
Fixes #16728.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant