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 / type guard fails to work in an inner function #13020

Closed
colinfang opened this issue Jun 25, 2022 · 1 comment
Closed

Type narrowing / type guard fails to work in an inner function #13020

colinfang opened this issue Jun 25, 2022 · 1 comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@colinfang
Copy link

Mypy: v0.961
Python: v3.10

def f(g: Callable[[str], None] | None = None) -> None:
    g = g or print
    g('a') # type guard works, mypy ok
    def h() -> None:
        g('a') # type guard fails, mypy thinks it could be `None`
    h()
@colinfang colinfang added the bug mypy got something wrong label Jun 25, 2022
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Jun 25, 2022
@JelleZijlstra
Copy link
Member

Duplicate of #2608

The other issue has an explanation of why this is tricky.

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