Idea: Support lazy imports and defer schema initialization
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 35/100
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
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de marshmallow-code/marshmallow-sqlalchemy
-
Dependency DashboardAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 45/100
-
Don't set a field as required if it's read-onlyPosiblemente ocupada @CyberneticX-Tech la tomó hace 64 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 45/100
-
I suspect a breaking change related to "include_relationships=True" and schema.load() in version 1.4.1Posiblemente ocupada @Fire162 la tomó hace 36 días. Abiertohelp wanted
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
marshmallow-code/marshmallow-sqlalchemy#664 · 5 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 25/100
marshmallow-code/marshmallow-sqlalchemy#580 · 1 comentario ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
Todos los issues de marshmallow-code/marshmallow-sqlalchemy
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
btclib-org/btclib#3062 ·
Los mantenedores suelen responder en 1 día
-
Run evals automatically in CIAbiertoarea:evals area:infra enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
jse-avl/support-bot#4 · 1 comentario ·
-
bug good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
qte77/agentic-job-offer-to-application-kit#487 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
On a free-threaded build, `_decrypted` can report a ledger locked mid-sign as a damaged fileAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
btclib-org/btclib-wallet#801 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
documentation good first issue
Dificultad 1/5 1-3 horas Aptitud para principiantes 94/100