Improve metaclass conflict error message with clearer terminology and contextual information
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
Improve Metaclass Conflict Error Message
Summary
Improve the error message raised during metaclass conflicts to specify the
base with incompatible metaclass.
Motivation
Class inheritance has a well-established terminology across languages, but
Python's metaclass system introduces a distinct mechanism that lacks clear
linguistic conventions. This feature proposes the following
terminologies.
- A class is derived from its metaclass
- A class is based on its base classes
- A metaclass is any subclass of type
Example
In the following code, MetaFoo and MetaBar are both subclasses of type
making them metaclasses according to the previous terminology suggestion.
Since neither is a subclass of the other, they are incompatible as metaclasses.
Foo derives from MetaFoo and Bar derives from MetaBar, but because
Bar is based on Foo; the conflicting metaclasses cause the TypeError
relating to metaclass conflict.
class MetaFoo(type): pass
class MetaBar(type): pass
class Foo(metaclass=MetaFoo): pass
class Bar(Foo, metaclass=MetaBar): pass # raises
Current Behaviour
The TypeError currently raised looks like this:
TypeError: metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases
Proposed Behaviour
The improved error message would add contextual details and adopt the new
terminology: (Line breaks included for clarity).
Metaclass conflict while creating a new class!
- Declared metaclass: 'MetaBar'
- Incompatible base class: 'Foo'
- That base is derived from metaclass: 'MetaFoo'
All base classes must be based on classes derived from the same metaclass or a
subclass thereof.
Notes
This change requires changes to the _PyType_CalculateMetaclass function only.
Including the name (Bar in the example) in the error message would require
changes to multiple functions across the codebase.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中提到的 _PyType_CalculateMetaclass 函数开始,并复现 MetaFoo、MetaBar、Foo 和 Bar 示例。将产生的 TypeError 与提议的措辞进行比较,其中应包括声明的元类、不兼容的基类及其元类;完成的标准是,该冲突使用所述术语报告这些上下文信息。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 45/100