Dataclasses: `No overload variant of "asdict" matches argument type "Self" [call-overload]` when deciding decorator conditionally

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

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

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
devtools

調査の方向性

提供された Python 3.12 の例を mypy 1.10.0 と --show-error-context --strict を使って実行し、dataclasses.asdict と dataclasses.replace のエラーを再現してください。まず、条件付き dataclass デコレーター、Self、および報告されたオーバーロードと型変数のチェックの処理を追跡します。これらの誤検出なしに例の型チェックが通り、実行時の動作が維持されれば完了です。

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

説明

feature topic-dataclasses

Bug Report

Mypy raises a false positive for dataclass.asdict (No overload variant of "asdict" matches argument type "Self" [call-overload]) on the very specific circumstances:

  1. The specific decorator for dataclass is decided based on a condition (e.g. Python version)
  2. The datalcasses.asdict method is called from a method that uses PEP 673's Self Type.

I also noted a similar problem occurs for dataclasses.replace: Value of type variable "_DataclassT" of "replace" cannot be "Self" [type-var].

To Reproduce

import sys
import dataclasses
import json
from typing import Self

if sys.version_info >= (3, 10):
    dataclass = dataclasses.dataclass(kw_only=True)
else:
    dataclass = dataclasses.dataclass


@dataclass
class HelloWorld:
    a: int = 1
    b: int = 2
    c: int = 3

    def double(self) -> Self:
        fields = dataclasses.asdict(self)
        return self.__class__(**{k: v*2 for k, v in fields.items()})

    def json_overwrite(self, text: str) -> Self:
        new = json.loads(text)
        return dataclasses.replace(self, **new)

x = HelloWorld().double()
y = HelloWorld().json_overwrite('{"c": 42}')
assert x.b == 4
assert y.c == 42
print(f"{x=}")
print(f"{y=}")

Gist URL: https://gist.github.com/mypy-play/70b801fbe15391f0750be8f6c403fdf4
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&flags=show-error-context%2Cstrict&gist=70b801fbe15391f0750be8f6c403fdf4

Expected Behavior

There should be no false negative.
The program works fine in runtime:

$ python3.12 main.py
x=HelloWorld(a=2, b=4, c=6)
y=HelloWorld(a=1, b=2, c=42)

Actual Behavior

main.py: note: In member "double" of class "HelloWorld":
main.py:19: error: No overload variant of "asdict" matches argument type "Self"  [call-overload]
main.py:19: note: Possible overload variants:
main.py:19: note:     def asdict(obj: DataclassInstance) -> dict[str, Any]
main.py:19: note:     def [_T] asdict(obj: DataclassInstance, *, dict_factory: Callable[[list[tuple[str, Any]]], _T]) -> _T
main.py: note: In member "json_overwrite" of class "HelloWorld":
main.py:24: error: Value of type variable "_DataclassT" of "replace" cannot be "Self"  [type-var]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: --show-error-context, --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

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

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

はじめの一歩

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

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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