Improve error message for return in metaclass __new__()
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Feature or enhancement
Proposal:
Case1: When overriding new in a custom Meta class and forgetting to return a class object (e.g. by omitting return super().new(..)) Python raises the following error:
TypeError: 'NoneType' object is not callable
Case2: When returning something else other than class object it raises following error:
TypeError: 'str' object is not callable
This messages, while technically correct, lacks guidance for common scenarios and can confuse developers - especially those new to Meta class.
Steps to Reproduce:
Case1:
class NewMeta(type):
def new(cls, name, bases, dct):
print ("Forgot to return!")
class MyClass(metaclass = NewMeta):
def greet(self):
pass
m = MyClass()
Output: TypeError: 'NoneType' object is not callable
Case2:
class NewMeta(type):
def new(cls, name, bases, dct):
return ("Return non-callable object")
class MyClass(metaclass = NewMeta):
def greet(self):
pass
m = MyClass()
Output: TypeError: 'str' object is not callable
Suggested Improvement:
Improve the error message to something like:
TypeError: metaclass new must return a class object. Recommendation: super().new(metacls, name, bases, namespace)?
This small change could save significant debugging time and make advanced features like metaclasses more approachable.
Python Version:
Tested in Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]
Why This Matters:
- Metaclass usage is already an advanced topic.
- Improving the DX (developer experience) here will help advanced learners and reduce friction.
- Similar improvements have already been adopted in recent Python versions for other common errors.
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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue で説明されている metaclass new の両方のケースを再現し、現在の TypeError を発生させるランタイムのエントリポイントを追跡します。戻り値がない場合、またはクラスでない場合の診断を更新し、次に両方の例のカバレッジを追加して、メッセージが必要な戻り値を明確に説明していることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100