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

Type narrowing not applied to a generator #13181

Closed
spagh-eddie opened this issue Jul 19, 2022 · 1 comment
Closed

Type narrowing not applied to a generator #13181

spagh-eddie opened this issue Jul 19, 2022 · 1 comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@spagh-eddie
Copy link

spagh-eddie commented Jul 19, 2022

Bug Report

An argument typed as None | str gets correctly narrowed to str after an if argument:, but the narrowing is not applied inside the if block.

To Reproduce

words = ["this", "that", "other"]


def fizz(mandatory_letters: None | str):
    filters = []
    reveal_type(mandatory_letters)
    if mandatory_letters:
        reveal_type(mandatory_letters)
        filters.append(lambda word: all(c in word for c in mandatory_letters))
    return filter(lambda word: all(f(word) for f in filters), words)

Expected Behavior

I expected no error.
I expected the type narrowing to apply to the if block and its generator.

Actual Behavior

% mypy file.py
file.py:6: note: Revealed type is "Union[None, builtins.str]"
file.py:8: note: Revealed type is "builtins.str"
file.py:9: error: Item "None" of "Optional[str]" has no attribute "__iter__" (not iterable)
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:
% mypy --version
mypy 0.961 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.1
  • Operating system and version: macOS Monterey 12.0.1 (21A559)
@spagh-eddie spagh-eddie added the bug mypy got something wrong label Jul 19, 2022
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Jul 19, 2022
@JelleZijlstra
Copy link
Member

This is an instance of #2608.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2022
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