Supporting attrs extensions with different default arguments for decorators.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
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
Beginne damit, das bereitgestellte Beispiel mit mypy's attrs plugin und den fake-plugin maker lists zu reproduzieren, insbesondere attr_class_makers und attr_attrib_makers. Verfolge, wie weggelassene Decorator-Argumente behandelt werden. Die Aufgabe ist erledigt, wenn benutzerdefinierte Decorator-Defaults berücksichtigt werden und der falsch-positive Fehler für ein Attribut ohne Default nicht mehr ausgegeben wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100