Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Special handling of descriptor field like in dataclasses

Aperta
#1,232 6 commenti 3 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

Start by reading the descriptor-typed fields documentation linked in the issue, then review the earlier discussions in #881 and #1108. Done means attrs supports descriptor fields without losing descriptor behavior during initialization or assignment, matching the documented dataclasses example.

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

Descrizione

dataclasses Feature

dataclass dataclasses handle descriptor fields differently from other fields, so the descriptor type is not lost when assigning:
https://docs.python.org/3/library/dataclasses.html#descriptor-typed-fields

This does not work in attrs, it's a pity because it could be used as an alternative to converters called with the instance (#1108).
Previous discussion about this was not very conclusive (#881).

# Example from python docs
from dataclasses import dataclass
from attrs import define


class IntConversionDescriptor:
    def __init__(self, *, default):
        self._default = default

    def __set_name__(self, owner, name):
        self._name = "_" + name

    def __get__(self, obj, type):
        if obj is None:
            return self._default

        return getattr(obj, self._name, self._default)

    def __set__(self, obj, value):
        setattr(obj, self._name, int(value))

# dataclass: WORKS AS EXPECTED
@dataclass
class InventoryItem:
    quantity_on_hand: IntConversionDescriptor = IntConversionDescriptor(default=100)

i = InventoryItem()
print(i.quantity_on_hand)   # 100
i.quantity_on_hand = 2.5    # calls __set__ with 2.5
print(i.quantity_on_hand)   # 2


# attrs: DOES NOT WORK
@define
class InventoryItem2:
    quanttity_on_hand: IntConversionDescriptor = IntConversionDescriptor(default=100)

i2 = InventoryItem2()
print(i2.quantity_on_hand)   # <__main__.IntConversionDescriptor object at 0x78c3a12a1250>
i2.quantity_on_hand = 2.5    # set InventoryItem2 attribute to a float, erasing the descriptor
print(i2.quantity_on_hand)   # 2.5

Lingua principale
Python
Stelle
5.9k
Fork
499
Merge medio
3g 12h
PR unite (30g)
3

Preparare l'ambiente

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.

Altre issue di python-attrs/attrs

Tutte le issue di python-attrs/attrs

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.