mypy failed to return correct overloaded func type when decorate func accepts more than one parameter
未关闭
还没有人认领这个 Issue。
bug
topic-overloads
topic-paramspec
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
Bug Report
mypy failed to return the correct overloaded func type when decorate func accepts more than one parameter.
To Reproduce
from functools import wraps
from typing import overload, Any, TypeVar, Callable, Type
from typing_extensions import ParamSpec, reveal_type
@overload
def foo(a: str, /) -> str:
pass
@overload
def foo(a: int, /) -> int:
pass
def foo(a: Any) -> Any:
return a
P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")
def forbid_return_type(
func: Callable[P, T],
tp: Type[Any],
/,
) -> Callable[P, T]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
result = func(*args, **kwargs)
if isinstance(result, tp):
raise TypeError(f"Return type {tp} is forbidden")
return result
return wrapper
foo_as_int = forbid_return_type(foo, str)
reveal_type(foo_as_int(1))
reveal_type(foo_as_int("1"))
Expected Behavior
note: Revealed type is "builtins.int"
note: Revealed type is "builtins.str"
Actual Behavior
note: Revealed type is "builtins.str"
error: Argument 1 has incompatible type "int"; expected "str" [arg-type]
note: Revealed type is "builtins.str"
Your Environment
- Mypy version used: mypy 1.14.0+dev
- Mypy command-line flags: -
- Mypy configuration options from
mypy.ini(and other config files): - - Python version used: 3.8.19
Both codes bellow work as expected:
from functools import wraps
from typing import overload, Any, TypeVar, Callable
from typing_extensions import ParamSpec, reveal_type
@overload
def foo(a: str, /) -> str:
pass
@overload
def foo(a: int, /) -> int:
pass
def foo(a: Any) -> Any:
return a
P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")
def forbid_return_type(
func: Callable[P, T],
) -> Callable[P, T]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
result = func(*args, **kwargs)
return result
return wrapper
foo_as_int = forbid_return_type(foo)
reveal_type(foo_as_int(1))
reveal_type(foo_as_int("1"))
from functools import wraps
from typing import Any, TypeVar, Callable, Type, Union
from typing_extensions import ParamSpec, reveal_type
def foo(a: Union[int, str]) -> Union[int, str]:
return a
P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")
def forbid_return_type(
func: Callable[P, T],
tp: Type[Any],
/,
) -> Callable[P, T]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
result = func(*args, **kwargs)
if isinstance(result, tp):
raise TypeError(f"Return type {tp} is forbidden")
return result
return wrapper
foo_as_int = forbid_return_type(foo, str)
reveal_type(foo_as_int(1))
reveal_type(foo_as_int("1"))
So it's not working only when the function accepts an overloaded function and another argument.
I checked with pyright and it correctly handles code.
/Users/yuriikarabas/projects/mypy/temp.py:45:13 - information: Type of "foo_as_int(1)" is "int"
/Users/yuriikarabas/projects/mypy/temp.py:46:13 - information: Type of "foo_as_int("1")" is "str"
0 errors, 0 warnings, 2 informations
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 mypy 运行最小复现,并将揭示出的类型与预期输出进行比较。当装饰器接收第二个参数时,跟踪被装饰函数的 overload 和 ParamSpec 推断过程。完成标准是:复现结果对 foo_as_int(1) 揭示 int,对 foo_as_int("1") 揭示 str,且没有参数类型错误。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100