Issue with `attrs` custom `__init__` defined

オープン 初心者向け
#20,818 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

評価

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

調査の方向性

mypy/plugins/attrs.py の345行目あたりから始め、クラスがすでに init を定義している場合に attrs.define が生成されるコンストラクターをどのように決定するかを追跡してください。Issue の A と B の例を再現し、その後、両方のカスタムコンストラクターが尊重され、期待される引数型エラーが報告されることを示す回帰テストを追加してください。

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

説明

bug topic-attrs

Bug Report
__init__ definition is ignored when generating attrs.define custom version.

To Reproduce


import attrs


@attrs.define
class A:
    x: int

    def __init__(self, x: str) -> None: ...


@attrs.define(init=False)
class B:
    x: int

    def __init__(self, x: str) -> None: ...

Expected Behavior

A(1) # error: Argument 1 to "A" has incompatible type "int"; expected "str"  [arg-type]
A("1") 
B(1) # error: Argument 1 to "B" has incompatible type "int"; expected "str"  [arg-type]
B("1")

Actual Behavior

A("1") # error: Argument 1 to "A" has incompatible type "str"; expected "int"  [arg-type]
B(1) # error: Argument 1 to "B" has incompatible type "int"; expected "str"  [arg-type]
B("1")

Your Environment

  • Mypy version used: mypy 1.20.0+dev
  • Mypy command-line flags: file.py --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): NA
  • Python version used: Python 3.13.9

Notes
I think the issue comes from:
https://github.com/python/mypy/blob/a86f7e3ab0ce17b9839239951430348e69c3284b/mypy/plugins/attrs.py#L345
as it does not check whether the __init__ method exists, it only checks that init=False when attrs.define is called.

主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 9時間
マージ済み PR(30日)
56

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

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

はじめの一歩

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

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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