Overloading a function with a parameter that can be either SomeType[Any] or Any always results in the return type Any
还没有人认领这个 Issue。
评估
调研方向
从 issue 中的 overload 示例开始,追踪 mypy 如何在类似 SomeType[Any] 的 overload 和 Any overload 之间进行选择。将 Callable/Any 情况与 int/Any 和 Callable/int 情况进行比较,然后验证修正后的行为会报告不兼容的赋值,同时保留现有的对比情况。
由索引模型根据 Issue 内容生成。
描述
Edit: I initially observed this behaviour for Callable[..., Any] and Any, but it now seems like this happens for any SomeType[Any] and Any, hence we've edited the title. For a more extensive explanation, scroll down to post 8.
I noticed that overloading a function with a Callable / Any parameter doesn't work as I'd expect. For example:
from typing import Any, Callable, overload
@overload
def foo(a: Callable) -> int: ...
@overload
def foo(a: Any) -> Any: ...
def foo(a):
pass
a: str = foo(lambda x: x)
I'd expect the mypy error Incompatible types in assignment (expression has type "int", variable has type "str"), but there's no error here.
Of note, if I replace Callable by something else, it works fine:
from typing import Any, overload
@overload
def foo(a: int) -> int: ...
@overload
def foo(a: Any) -> Any: ...
def foo(a):
pass
a: str = foo(1)
# mypy error: Incompatible types in assignment (expression has type "int", variable has type "str")
Finally, if I instead replace Any by something else, it also works fine.
from typing import Any, Callable, overload
@overload
def foo(a: Callable) -> int: ...
@overload
def foo(a: int) -> Any: ...
def foo(a):
pass
a: str = foo(lambda x: x)
# mypy error: Incompatible types in assignment (expression has type "int", variable has type "str")
So it only doesn't work as expected in the first code block. Is that a bug? Or is it expected behaviour somehow?
- 主要语言
- 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
-
link-check link-check:sphinx-theme
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
难度 2/5 1-3 小时 新手友好度 88/100
OpenHands/extensions#626 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
CSCfi/sd-search-api#39 ·
-
难度 1/5 1 小时以内 新手友好度 90/100