Removing Schema context feels like a bad idea!

Aperta
#2,832 3 commenti 0 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
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia leggendo l’issue #2707 e la pull request a cui fa riferimento, quindi confronta la documentazione attuale del contesto v4 con gli esempi di self.context descritti qui. Determina se il contesto passato alle chiamate dump/load o tramite un context manager è supportato e registra la sostituzione o la decisione prevista dal progetto per l’onere di migrazione segnalato.

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

Descrizione

Maybe this is subjective, but it feels like the removal of context makes previously simple tasks much more cumbersome and verbose...

Correct my if I'm wrong, but it looks like you expect context to be completely handled outside marshmallow using basically global variables (yes, I know, contextvars are better than that, but both are similarly "loose").

For example, quite a few schemas require access to user for which the dumping happens, e.g. to filter out certain data post-dump or to include information on whether the user can do certain operations with the object being dumped. In both cases, I've been using MySchema(context={'user': session.user}) so the schema itself is agnostic to request state - any user could be passed in via the schema, not necessarily the currently logged-in user. Inside the schema it was trivial to use self.context[...] in Method fields or hooks.

Now, however, unless I'm missing something, ALL this functionality is gone, and I'm expected to create a new class for the context, and import it both for passing context and for receiving it (YAY for circular import issues, they were already bad enough before especially when using SQLAlchemy model schemas). It all becomes much more verbose.

A simple self.context['user'] to access it vs something like Context[UserContext].get()['user'] or Context[UserContext].get() (haven't actually tested v4 yet, so not sure how exactly it works).

And honestly, I'm absolutely not looking forward to fixing over 50 occurrences of self.context plus roughly the same number of places where it's being passed to a schema...

The description of #2707 still mentions that "Context can be passed in dump/load call or set in a context manager" but I can't see this in the docs nor the PR's code. So this never happened I guess?

Lingua principale
Python
Stelle
7.2k
Fork
790
Merge medio
10h 40m
PR unite (30g)
6

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 marshmallow-code/marshmallow

Tutte le issue di marshmallow-code/marshmallow

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.