Idea: Support lazy imports and defer schema initialization

Abierto
#752 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python, sqlalchemy
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
580
Forks
105
Merge medio
9 h 8 min
PR fusionados (30 d)
3

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de marshmallow-code/marshmallow-sqlalchemy

Todos los issues de marshmallow-code/marshmallow-sqlalchemy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.