python / python/typing

Clarify the float/int/complex special case

未关闭
#1,746 27 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic: typing spec
主要语言
Python
星标
1.8k
派生
302
平均合并
23 小时
30 天内合并 PR
8

描述

The typing spec currently says this:

Python’s numeric types complex, float and int are not subtypes of each other, but to support common use cases, the type system contains a straightforward shortcut: when an argument is annotated as having type float, an argument of type int is acceptable; similar, for an argument annotated as having type complex, arguments of type float or int are acceptable.

This is helpful in that it clarifies that there is not a subtype relationship here. It remains unclear (to me, at least) in the following ways:

  1. It implies that this special case applies only to function argument annotations, and not to any other annotation (e.g. an annotation of the type of an attribute of a class). I find this surprising, but perhaps it is intended? I think it is surprising enough that if it's intended, the wording should be even clearer, and explicitly show examples where the special case does not apply outside an argument annotation.
  2. The wording "is acceptable" in the absence of a subtype relationship does not clarify how this special case actually fits into the type system; we can accept an int to a float typed argument, but then how do we type that name within the function body? If what we actually mean here is that float should be interpreted as float | int, then we should say that clearly.

There was some discussion of this on https://github.com/python/typing/issues/1663, which is about the different but related question of whether to mention the numeric tower and PEP 3141.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

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

调研方向

从 typing spec 中引用的 numeric-types 部分开始,并审阅 typing/issues/1663 中的相关讨论。确定该快捷方式是否仅适用于函数参数,以及在函数内部如何理解所接受的类型,然后用示例和对 "done" 含义的精确定义更新措辞。

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

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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