Issue with `attrs` custom `__init__` defined
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in mypy/plugins/attrs.py, intorno alla riga 345, e segui il modo in cui attrs.define determina il costruttore generato quando una classe definisce già init. Riproduci gli esempi A e B dell'issue, quindi aggiungi una copertura di regressione che dimostri che entrambi i costruttori personalizzati vengono rispettati e che vengono segnalati gli errori di tipo degli argomenti previsti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools, tooling
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 62/100