Skip to content

import xxx as var does not narrow type #14957

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

Closed
spagh-eddie opened this issue Mar 25, 2023 · 2 comments
Closed

import xxx as var does not narrow type #14957

spagh-eddie opened this issue Mar 25, 2023 · 2 comments
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@spagh-eddie
Copy link

Bug Report

I would expect a variable that gets imported narrow the type.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=11a4883cf29edd47fd1aff15302ead30

x: str | None = None
if x is None:
    from os import sep as x
# revealed type is Optional[str]
print(x + "a")  # mypy false positive

Expected Behavior

No error.

Actual Behavior

% mypy test.py
main.py:5: error: Unsupported left operand type for + ("None")  [operator]
main.py:5: note: Left operand is of type "Optional[str]"
Found 1 error in 1 file (checked 1 source file)
% python test.py
/a

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags: none
  • Python version used: 3.11
@spagh-eddie spagh-eddie added the bug mypy got something wrong label Mar 25, 2023
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Mar 25, 2023
@AlexWaygood
Copy link
Member

Duplicate of #12965

@AlexWaygood AlexWaygood marked this as a duplicate of #12965 Mar 25, 2023
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2023
@spagh-eddie
Copy link
Author

Duplicate of #12965

nice! I didn't find it... I assume because searching "import" has many false positives

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-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants