python / python/mypy

Clarification of `properly subclassable class` in NewType

未关闭
#6,701 6 条评论 12 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug documentation false-positive priority-1-normal topic-newtype
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

I want to create a new type derived from ObjectId in similar way as is described in documentation for new type UserId derived from int:

from bson import ObjectId
UserId = NewType('UserId', ObjectId)

But Mypy gives Argument 2 to NewType(...) must be subclassable (got "Any").

There is written in documentation:

NewType accepts exactly two arguments. The first argument must be a string literal containing the name of the new type and must equal the name of the variable to which the new type is assigned. The second argument must be a properly subclassable class, i.e., not a type construct like Union, etc.

It is not clear what properly subclassable class means and why ObjectId is not properly subclassable class. Could please someone clarify this to me and in documentation?

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 NewType 文档部分开始,并使用 ObjectId 重现该诊断。阐明“可正确进行子类化的类”是什么意思,并解释为什么 ObjectId 被报告为 Any,然后验证修订后的措辞是否处理了该示例并保留现有的 NewType 指导。

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

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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