Skip to content

import xxx as var does not narrow type #14957

Closed as not planned
Closed as not planned
@spagh-eddie

Description

@spagh-eddie

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions