python / python/typeshed

Replace `float` with `float | int`

未关闭
#16,059 5 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

project: policy
主要语言
Python
星标
5.1k
派生
2.1k
平均合并
1 天 19 小时
30 天内合并 PR
82

描述

As discussed in python/typing#1748: I propose to (slowly) replace float with float | int and complex with complex | float | int, where applicable.

  • It's more correct, even if float is current implicitly equivalent to int | float.
  • We are already prepared should the implicit promotion ever be removed.
  • We give type checkers the ability to optionally disable the promotion so that type checker authors and users can experiment with it.
  • Typeshed would always need to be the first step for this, so let's remove this blocker.

This issue proposes an ugly and slow, but correct and safe procedure:

  1. Disable flake8-pyi code Y041.
    #16096
  2. Introduce two type aliases FloatInt = float | int and ComplexInt = complex | float | int to _typeshed.
    #16068
  3. Globally replace all instances of float with FloatInt and of complex with ComplexInt in the stdlib.
  4. After 2026-09-22 we do the same for third-party stubs.
  5. Review FloatInt and ComplexInt instances are replace them with the proper types. (This will take a while.)
  6. Remove the type aliases once they are not needed anymore (in 10 years or so ...)

Using FloatInt and ComplexInt would be disallowed for new code and the type aliases should explicitly be marked as not to be used by non-typeshed code.

We should also probably add flake8-pyi checks that checks that argument types use float | int and complex | float | int. Cases where only float/complex is allowed are probably rare.

贡献指南

打开贡献指南

从这里开始

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

调研方向

先从 stdlib 存根以及 _typeshed 的 FloatInt 和 ComplexInt 别名开始,然后检查 flake8-pyi 的 Y041 配置。在整个 stdlib 中搜索 float 和 complex 注解,并审查每个候选项;完成意味着实施计划中的 stdlib 全范围迁移,而第三方存根仍推迟到 2026-09-22 之后。

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

评估

技术栈
python
领域
tooling
Issue 类型
重构
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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