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 widens in nested function #12979

Closed
randolf-scholz opened this issue Jun 13, 2022 · 1 comment
Closed

Type widens in nested function #12979

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

Comments

@randolf-scholz
Copy link
Contributor

randolf-scholz commented Jun 13, 2022

Bug Report

Despite an if x is not None check, mypy suddenly believes x could be None inside a function definition.

To Reproduce

from functools import wraps
from collections.abc import Callable
from typing import TypeVar, ParamSpec, Optional

P = ParamSpec("P")
R = TypeVar("R")


def foo(func: Callable[P, R], before_func: Optional[Callable[P, None]] = None) -> Callable[P, R]:

    if before_func is not None:

        reveal_type(before_func)  # <--  Revealed type is "def (*Any, **Any) -> Any"

        @wraps(func)  # problem persists when removing @wraps
        def wrapper(*args, **kwargs):
            reveal_type(before_func)  # Revealed type is "Union[def (*Any, **Any) -> Any, None]"
            before_func(*args, **kwargs)  # error: "None" not callable
            return func(*args, **kwargs)

        return wrapper

    return func

Your Environment

  • Mypy version used: mypy 0.961 (compiled: yes)
  • Python version used: Python 3.10.5
  • Operating system and version: Ubuntu 20.04.4 LTS
Mypy configuration
[tool.mypy]
allow_redefinition = true
color_output = true
error_summary = true
files = ["src/", "test/"]
plugins = "numpy.typing.mypy_plugin"
pretty = true
python_version = "3.10"
check_untyped_defs = true
show_column_numbers = true
show_error_codes = true
warn_unused_ignores = true
#disallow_untyped_defs = true
disallow_incomplete_defs = true
@randolf-scholz randolf-scholz added the bug mypy got something wrong label Jun 13, 2022
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Jun 13, 2022
@AlexWaygood
Copy link
Member

Duplicate of #2608

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

3 participants