python / python/mypy

Overloaded `__init__` prevents type from being recognized as an attrs class

Offen
#19,003 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-attrs
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

When an attrs class has and overloaded __init__ function, mypy does not consider it to be an attrs class when evaluating attrs.evolve.

To Reproduce

from typing import overload

import attrs


@attrs.frozen(init=False)
class C:
    x: int | str

    @overload
    def __init__(self, x: int) -> None: ...

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

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


obj = C(1)
attrs.evolve(obj, x=2)  # error

Expected Behavior

The C instance should be an acceptable input to attrs.evolve.

Actual Behavior

$ mypy t.py
t.py:21: error: Argument 1 to "evolve" has incompatible type "C"; expected an attrs class  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.15.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.13.0 (v3.13.0:60403a5409f, Oct 7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)]

Notes

This appears to be because the init method is both not a FuncDef (it is OverloadedFuncDef) and its type attribute is not a CallableType. See: https://github.com/python/mypy/blob/7b4f6311e29452cc8d4ddb78331d0047c8b17e93/mypy/plugins/attrs.py#L1033-L1034

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne bei mypy/plugins/attrs.py um die Zeilen 1033–1034 und reproduziere das Problem mit dem bereitgestellten Snippet mit überladenem init unter Verwendung von mypy 1.15.0. Die Arbeit ist abgeschlossen, wenn attrs.evolve die C-Instanz ohne den gemeldeten Inkompatibler-Typ-Fehler akzeptiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.