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

Dict in does not narrow down Union #14859

Closed
james10424 opened this issue Mar 8, 2023 · 1 comment
Closed

Dict in does not narrow down Union #14859

james10424 opened this issue Mar 8, 2023 · 1 comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder

Comments

@james10424
Copy link

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

from typing import Dict, Union, NewType

A = NewType("A", str)
B = NewType("B", str)

dict_a: Dict[A, str] = {}

def f(a: Union[A, B]):
    if a in dict_a:
        dict_a[a]

Expected Behavior

Mypy recognizes that a inside the if statement is type A, because dict_a's keys are of type A and a is one of the keys.

Actual Behavior

error: Invalid index type "Union[A, B]" for "Dict[A, str]"; expected type "A"

Your Environment

  • Mypy version used: v1.1.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10
@james10424 james10424 added the bug mypy got something wrong label Mar 8, 2023
@AlexWaygood AlexWaygood added feature topic-type-narrowing Conditional type narrowing / binder and removed bug mypy got something wrong labels Mar 8, 2023
@AlexWaygood
Copy link
Member

Duplicate of #10977

@AlexWaygood AlexWaygood marked this as a duplicate of #10977 Mar 8, 2023
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants