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

Mypy does not correctly infer type of union typed variable on asignment when on same line as type declaration #17856

Closed
tblum opened this issue Oct 1, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@tblum
Copy link

tblum commented Oct 1, 2024

Mypy does not correctly infer the type of in the following example
i: int | None = 42
i's type is int | None

when splitting declaration and assignment it works i.e:

i: int | None
i = 42

i's type is int

i: int | None = 42
makes for nicer code.

To Reproduce

i: int | None = 42
i = i + 1

Expected Behavior
Should be accepted by mypy

Actual Behavior

inone_test.py:2: error: Unsupported operand types for + ("None" and "int") [operator]
none_test.py:2: note: Left operand is of type "int | None"

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12.6
@tblum tblum added the bug mypy got something wrong label Oct 1, 2024
@hauntsaninja
Copy link
Collaborator

Duplicate of #2008

(there's consensus mypy should change this and it's a trivial change to make, but it exposes some bugs in the binder)

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Oct 1, 2024
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

No branches or pull requests

2 participants