Idea: Support lazy imports and defer schema initialization

未关闭
#752 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
python, sqlalchemy
领域
backend

调研方向

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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

marshmallow-code/marshmallow-sqlalchemy 的其他 Issue

查看 marshmallow-code/marshmallow-sqlalchemy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。