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

Un-narrowing optional type when defining nested function within a conditional #15251

Closed
mishok13 opened this issue May 16, 2023 · 2 comments
Closed
Labels
bug mypy got something wrong

Comments

@mishok13
Copy link

Bug Report

When defining a nested function under a branch that accesses argument passed to outer function, the type narrowing resets to the original type.

To Reproduce

See the playground. Code posted here as well.

from functools import wraps
from typing import Any, Callable

def d(f: Callable[..., Any] | None) -> None:
    if f is not None: # <-- narrowed to Callable
        def inner() -> Any:
            return f() # <-- resolved as Callable | None
    return None

Expected Behavior

Inner function retains narrowed type.

Actual Behavior

Function f's type is resolved as Callable | None leading to this error:

main.py:9: error: "None" not callable  [misc]

Your Environment

  • Mypy version used: 1.2, 1.3
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): no additional config
  • Python version used: 3.11, 3.10
@mishok13 mishok13 added the bug mypy got something wrong label May 16, 2023
@AlexWaygood
Copy link
Member

Duplicate of #2608

@AlexWaygood AlexWaygood marked this as a duplicate of #2608 May 16, 2023
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2023
@AlexWaygood
Copy link
Member

(Your code type checks fine on mypy master. The fix for #2608 will be available in the next release of mypy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants