Idea: Support lazy imports and defer schema initialization
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi 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.
Altre issue di marshmallow-code/marshmallow-sqlalchemy
-
Dependency DashboardAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 45/100
-
Don't set a field as required if it's read-onlyForse già presa @CyberneticX-Tech l’ha presa 64 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 45/100
-
I suspect a breaking change related to "include_relationships=True" and schema.load() in version 1.4.1Forse già presa @Fire162 l’ha presa 36 giorni fa. Apertahelp wanted
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
marshmallow-code/marshmallow-sqlalchemy#664 · 5 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
marshmallow-code/marshmallow-sqlalchemy#580 · 1 commento ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Tutte le issue di marshmallow-code/marshmallow-sqlalchemy
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
btclib-org/btclib#3062 ·
I maintainer di solito rispondono entro 1 giorno
-
area:evals area:infra enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
jse-avl/support-bot#4 · 1 commento ·
-
bug good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
qte77/agentic-job-offer-to-application-kit#487 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
btclib-org/btclib-wallet#801 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
documentation good first issue
Difficoltà 1/5 1-3 ore Idoneità per principianti 94/100