Bug: overloads not matched when using `TypeVar` with `default` (where Pyright matches correctly)
还没有人认领这个 Issue。
评估
调研方向
该 issue 没有指明源文件或测试。先从提供的 Python 3.12 复现开始,使用 mypy 1.15.0 运行它,并将 reveal_type 的结果与 Pyright 进行比较;对于这个案例,当重载解析为 Bar 而不是 Any 时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Bug Report
In the example below, mypy reveals the type to be Any, whereas Pyright correctly detects Bar
Note that replacing T | None with T makes Mypy also infer the type correctly
Noticed while working on pandas-stubs
To Reproduce
from typing import Any, Callable, Generic, Protocol, Self, overload, reveal_type
from typing_extensions import TypeVar
T = TypeVar("T", bound=str | int, default=Any)
class Foo(Generic[T]):
def __init__(self, t: T) -> None:
self.t = t
class Bar(Protocol):
@overload
def apply(self, f: Callable[..., Foo]) -> Self: ...
@overload
def apply(self, f: Callable[..., T | None]) -> Foo[T]: ...
def func() -> Foo:
return Foo(3)
def main(b: Bar) -> None:
reveal_type(b.apply(func))
https://mypy-play.net/?mypy=latest&python=3.12&gist=6df33ff32887ddfab4c1ae43edd62b2d
Expected Behavior
that reveal_type would show Bar (for reference, this is what Pyright does)
Actual Behavior
note: Revealed type is "Any"
Your Environment
- Mypy version used: 1.15.0
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.12
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
-
bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#759 ·