python / python/cpython

Better support for descriptors at dataclass creation

Aperta
#144,749 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-dataclasses type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

Hello, I propose a change to the way dataclass works with fields defined using a descriptor class, particularly the way a default value is assigned.

What is the issue

According to the docs:

To determine whether a field contains a default value, @dataclass will call the descriptor’s get() method using its class access form: descriptor.get(obj=None, type=cls). If the descriptor returns a value in this case, it will be used as the field’s default. On the other hand, if the descriptor raises AttributeError in this situation, no default value will be provided for the field.

This behaviour is an issue for descriptors that need to be referenced at class creation: a common pattern in that case is to define the __get__ method with a test so that the descriptor returns itself outside of a call on an instance of the class:

class Descriptor:
    ...
    def __get__(self, instance, instance_type=None):
        if instance is None:
            return self
        ...
    ...

In that case, when the dataclass decorator checks for a default value then self is returned, which means that:

  1. The field has a default value, when it probably shouldn't,
  2. The default value is a descriptor instance, which is almost definitely wrong.

Please note that the property builtin is such a descriptor, this is not an edge case. Here is an example using it:

from dataclasses import dataclass
from operator import attrgetter

@dataclass
class A:
    a: int = property(attrgetter('_a'))

    @a.setter
    def a(self, val):
        self._a = val

Then:

>>> A(1) # expected behaviour
A(a=1)

>>> A() # unexpected behaviour, but no exception is raised
A(a=<property object at 0x0000027C27023BA0>)
What could be changed ?

I propose adding a check to verify if __get__ returns the descriptor instance itself, to prevent it from using that instance as a default value. However, I am not well versed in the dataclass creation mecanisms, so I am not aware of any consequence it could have.

Is this a breaking change ?

Technically yes. I can't think of any applications where this behaviour could be used, and I don't know how to check if any project on github uses it, but it doesn't change the fact that it breaks backward compatibility.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-144940

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 dalla creazione di dataclass e dal comportamento di get descritto nell’issue. Esamina la PR collegata gh-144940 e verifica come i risultati dell’accesso alla classe vengono trattati come valori predefiniti; il lavoro è completato quando le istanze di descriptor non vengono usate erroneamente come valori predefiniti dei campi senza compromettere il comportamento previsto del descriptor.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.