Idea: Support lazy imports and defer schema initialization

Aperta
#752 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python, sqlalchemy
Ambito
backend

Direzione di ricerca

Start by reading the schema model configuration and initialization paths in marshmallow-sqlalchemy, then compare them with the proof of concept in the issue. Determine whether lazy model resolution can be supported without triggering mapper setup or circular imports during module import. Done means schemas can defer initialization until instantiation while preserving existing behavior; the issue does not name specific files or tests.

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

Descrizione

The idea is to pass a lazy_import proxy as the model of a Schema and only trigger the schema initialization the first time it's instantiated.

This would avoid triggering SQLAlchemy mapper setup during import time which can cause problems when not all models have been imported yet at that point, and also avoid issues with circular imports between modules defining schemas and modules defining db models.

Here's a small proof of concept how it could work (w/o any MM code, just to show the general idea):

# schemas.py
print('schemas.py import start')
import typing
from types import LazyImportType

lazy from models import SomeModel


class Schema:
    def __init__(self):
        self.model = getattr(self, 'model', None) or typing.get_type_hints(type(self))['model']
        # only needed when suing the `global()` variant below; with the type int it's being resolved above
        if isinstance(self.model, LazyImportType):
            self.model = self.model.resolve()

    def __repr__(self):
        return f'<{type(self).__name__}({self.model.__name__})>'


class SomeSchema(Schema):
    # doesn't work since it triggers reification:
    # model = SomeModel

    # this works but it's ugly
    # model = globals()['SomeModel']

    # this also works and is not super ugly, but a bit weird to use a type annotation for this
    model: SomeModel


def main():
    print('schemas.main - instantiating SomeSchema')
    print(SomeSchema())


print('schemas.py import done')

if __name__ == '__main__':
    main()
# models.py
print('models.py import start')

class SomeModel:
    pass

print('models.py import done')

When running it:

$ python lazytest/schemas.py
schemas.py import start
schemas.py import done
schemas.main - instantiating SomeSchema
models.py import start
models.py import done
<SomeSchema(SomeModel)>

The somewhat ugly part is of course that accessing a lazy import by name immediately triggers the actual import, I don't think Python has a nice way to pass this imported symbol without it being reified. The easiest way to avoid it is accessing it via globals()['SomeModel'] but of course that's extremely ugly.

The best alternative would be using a type annotation, ie model: SomeModel instead of model = SomeModel. Those only trigger reification of the import when getting the type hints of the object.

Lingua principale
Python
Stelle
580
Fork
105
Merge medio
9h 8m
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 marshmallow-code/marshmallow-sqlalchemy

Tutte le issue di marshmallow-code/marshmallow-sqlalchemy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.