Issue with `attrs` custom `__init__` defined
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie in mypy/plugins/attrs.py etwa bei Zeile 345 und verfolgen Sie, wie attrs.define den generierten Konstruktor bestimmt, wenn eine Klasse bereits init definiert. Reproduzieren Sie die A- und B-Beispiele aus dem Issue und fügen Sie anschließend Regressionstests hinzu, die zeigen, dass beide benutzerdefinierten Konstruktoren berücksichtigt werden und die erwarteten Argumenttypfehler gemeldet werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools, tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 62/100