python / python/mypy

Supporting attrs extensions with different default arguments for decorators.

Aperta
#12,774 4 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive feature topic-attrs
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

attrs' decorators can (and sometimes should) be wrapped, to create custom decorators. Using a "fake plugin", the custom decorators can be added to the maker lists and thus receive the special attrs treatment from mypy.
However, currently, mypy's attrs plugin has the default values of the arguments of attrs' decorators hardcoded. This means that it does not detect when the custom decorators define different default values, and thus raises (false positive) errors on some cases.

Here is an example for such a case:

import attr
from typing import Any

def my_attr_s(cls, kw_only: bool = True) -> Any:  # Added to attr_class_makers using "fake plugin".
    return attr.s(kw_only=kw_only)(cls)

def my_attr_ib(**kwargs) -> Any:  # Added to attr_attrib_makers using "fake plugin".
    return attr.ib(**kwargs)

@my_attr_s
class A:
    optional: str = my_attr_ib(default="This attrib is not required now.")

@my_attr_s
class B(A):
    required: str = my_attr_ib()

When type-checking this file, mypy gives the following error:

16: error: Non-default attributes not allowed after default attributes.

This error should actually not be emitted, because as opposed to what would happen if we we decorate A and B with attr.s, there is no problem here, because here we have kw_only set to True (by default, and nothing else was passed), so the order of the attributes is actually valid.

If mypy would take the decorator's arguments' default values for arguments that were not passed, it could avoid such false positives.
I would like to submit a PR which fixes this.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l'esempio fornito con mypy's attrs plugin e le fake-plugin maker lists, in particolare attr_class_makers e attr_attrib_makers. Traccia il modo in cui vengono gestiti gli argomenti omessi del decoratore. Il lavoro è completato quando vengono rispettati i valori predefiniti personalizzati del decoratore e non viene più emesso l'errore falso positivo relativo a un attributo senza valore predefinito.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.