Closed as not planned
Description
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