Idea: Support lazy imports and defer schema initialization
还没有人认领这个 Issue。
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Python
- 星标
- 580
- 派生
- 105
- 平均合并
- 9 小时 8 分钟
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
marshmallow-code/marshmallow-sqlalchemy 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 45/100
-
Don't set a field as required if it's read-only可能已有人在做 @CyberneticX-Tech 于 64 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 45/100
-
I suspect a breaking change related to "include_relationships=True" and schema.load() in version 1.4.1可能已有人在做 @Fire162 于 36 天前认领。 未关闭help wanted
难度 4/5 3-5 天 新手友好度 35/100
marshmallow-code/marshmallow-sqlalchemy#664 · 5 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
marshmallow-code/marshmallow-sqlalchemy#580 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
查看 marshmallow-code/marshmallow-sqlalchemy 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
btclib-org/btclib#3062 ·
维护者通常 1 天内回复
-
area:evals area:infra enhancement
难度 2/5 1-3 小时 新手友好度 74/100
jse-avl/support-bot#4 · 1 条评论 ·
-
bug good first issue
难度 2/5 1-3 小时 新手友好度 85/100
qte77/agentic-job-offer-to-application-kit#487 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
btclib-org/btclib-wallet#801 · 1 条评论 ·
维护者通常 1 天内回复
-
documentation good first issue
难度 1/5 1-3 小时 新手友好度 94/100