Add original attribute fullname to `AttributeContext` for `get_attribute_hook`
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
Feature
Allow a get_attribute_hook plugin to see the fullname which was used inside of the hook method.
Currently, the interface for a plugin which handles custom __getattr__ looks like so:
def hook_func(ctx: AttributeContext) -> Type:
...
class MyPlugin(Plugin):
def get_attribute_hook(self, fullname: str) -> ...:
return hook_func
However, this loses fullname in the context of hook_func.
I've been reading through the various attributes of AttributeContext, and it appears that fullname is lost.
It would be great, for this specific case, to have access to ctx.fullname, as the name which was used may be of particular interest to hook_func.
Pitch
I'm exploring a plugin which handles the types of an object which defines a custom __getattr__.
The trouble is that the object is basically a proxy (#5523), and therefore the type of any given attribute depends on its name.
In #5523, I provided a toy example of a "listifying" proxy type.
I should also note my real use-case, which is more interesting and better justified. At work, I defined a proxy-like object which handles paginated web APIs:
https://github.com/globus/globus-sdk-python/blob/cf91ae8b1b613997b5d706a85a4602df3126e5e9/src/globus_sdk/paging/table.py#L61
This is meant to provide X.paginated.foo as a wrapper over X.foo which iterates over multiple X.foo calls.
I was able to define a plugin which handles this by making the proxy-like object a generic whose type-argument is the class which it wraps. The plugin can then check foo in the proxy against foo in the type-argument, and derive the correct type information. However, this requires access to the name foo, which does not appear to be otherwise available. The only way I could solve was by making get_attribute_hook return a new callable for each name it sees, acting as a closure over that name. i.e.
def hook_gen(name: str):
def hook_func(ctx: AttributeContext) -> Type:
...
return hook_func
class MyPlugin(Plugin):
def get_attribute_hook(self, fullname: str) -> ...:
return hook_gen(fullname)
This is inefficient and unintuitive. It would be better to have the fullname available without this layer of indirection.
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 individuando AttributeContext e il punto di ingresso del plugin get_attribute_hook, quindi esamina i test esistenti della plugin API per gli attribute hook. Aggiungi l’accesso al fullname usato dall’hook e coprilo con un test di regressione; il lavoro è completato quando un hook può leggere il nome dell’attributo richiesto senza creare una closure.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100