python / python/mypy

Any is inferred with a certain combination of overloads and generics

未关闭
#17,331 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

Bug Report

(I couldn't come up with a better title, please fix)

I've got a piece of code where the inferred type is sometimes Any when it shouldn't be, and the difference between getting Any and the correct result doesn't make sense so it looks like a mypy bug to me. I may be wrong though.

The background: I'm trying to refactor and fix the overloads for https://github.com/scrapy/scrapy/blob/2b9e32f1ca491340148e6a1918d1df70443823e6/scrapy/utils/defer.py#L360, I've simplified the overloads and ignored the "overlapping signatures" error per https://stackoverflow.com/questions/60222982/type-annotation-for-overloads-that-exclude-types-something-vs-everything-else but the inferred result of the function works fine only if the input coroutine doesn't return e.g. List[Any]. It doesn't seem to fall back to the second overload either, as that wouldn't result in Any too. Overall it doesn't look like something with my code but maybe I'm missing something.

To Reproduce

https://gist.github.com/mypy-play/29b3eb14ab12db21dc8f52594f40c392

Expected Behavior

I expect the revealed type to always be a Deferred with the same param as the coroutine result type and never Any.

Actual Behavior

It's Any when, it looks like, the argument type has Any in it.

Your Environment

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 mypy-play 复现和 gist 开始,比较 coroutine 结果包含 Any 与包含精确类型时的重载解析和揭示出的类型。在 mypy 1.10.0 和 Python 3.12 下检查行为;完成的标准是,揭示出的结果始终保留 coroutine 结果类型,而不是变成 Any。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
compilers, devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。