python / python/mypy

Incorrect __post_init__ order when KW_ONLY used

オープン
#17,564 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

from dataclasses import dataclass, InitVar, KW_ONLY


@dataclass
class A:
    _: KW_ONLY
    x: InitVar[int]

    def __post_init__(self, x: int) -> None:
        pass


@dataclass
class C(A):
    y: InitVar[str]

    def __post_init__(self, x: int, y: str) -> None:  # Correct order.
        super().__post_init__(x)

gives

a.py:19: error: Argument 2 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "str"  [override]
        def __post_init__(self, x: int, y: str) -> None:
                                ^~~~~~
a.py:19: error: Argument 3 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "int"  [override]
        def __post_init__(self, x: int, y: str) -> None:

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず報告された Python の例を再現し、post_init に関係する 2 つの override エラーを確認します。次に、引数の順序がどこで導出されるのかを特定するために、KW_ONLY と InitVar に対する mypy の dataclass 処理を追跡します。例がそれらのエラーなしで型チェックを通過し、示されている post_init の順序が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。