Allow method assignment in body of type extending typing.NamedTuple
オープン
まだ誰も着手していません。
bug
priority-2-low
topic-named-tuple
topic-runtime-semantics
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
this currently works for non-namedtuple classes (both runtime and mypy-time):
class C:
x: int
__hash__ = object.__hash__
and it works for typing.NamedTuple at runtime:
from typing import NamedTuple
class C(NamedTuple):
x: int
__hash__ = object.__hash__
$ python3 -i t2.py
>>> c = C(1)
>>> hash(c)
-9223363269847169460
>>> object.__hash__(c)
-9223363269847169460
>>> tuple.__hash__(c)
3430019387558
but fails at type-checking time:
$ mypy --version && python --version --version
mypy 0.770
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0]
$ mypy t2.py
t2.py:7: error: NamedTuple field name cannot start with an underscore: __hash__
t2.py:7: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
Found 2 errors in 1 file (checked 1 source file)
concrete usecase: __hash__ is in the critical path (~13%) for some code I'm writing and the extra layer of function which calls into object.__hash__'s overhead is significant:
# mypy is ok with this
from typing import NamedTuple
class C(NamedTuple):
x: int
def __hash__(self) -> int:
return object.__hash__(self)
I'd be happy to hack on this given some pointers :)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
t2.py の例で問題を再現し、ファイルに対して mypy を実行して hash に関する 2 つの診断を確認してください。NamedTuple のクラス本体がフィールドとメソッドの代入をどのように区別しているかを追跡し、そのうえで、無効なフィールド宣言に対する検証を維持しながら、代入が型チェックを通るようにしてください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100