tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.68k stars 392 forks source link

pydantic v2,computed cannot use attribute from exclude #1464

Open liu-junyong opened 1 year ago

liu-junyong commented 1 year ago

modify from example: https://github.com/tortoise/tortoise-orm/blob/develop/examples/fastapi/models.py show full_name and hide name:

    class PydanticMeta:
        computed = ["full_name"]
        exclude = ["password_hash","name"]
raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')

AttributeError: 'User' object has no attribute 'name'

Describe the bug tortoise-orm: v0.20.0 pydantic : v2.3.0

Expected behavior like pydantic v1, computed first then exclude

plusiv commented 1 year ago

Seems to be the same as https://github.com/tortoise/tortoise-orm/issues/1440

liu-junyong commented 1 year ago

up to now:

pydantic-core: 2.7 fix: https://github.com/pydantic/pydantic-core/pull/934

pydantic: last resource: with commit: https://github.com/pydantic/pydantic/commit/2575e71894a32dc615f9fa7a94ff37bb604a48a8

run result:

File "/Users/liujunyong/python310env/lib/python3.10/site-packages/tortoise/contrib/pydantic/creator.py", line 377, in pydantic_model_creator annotation = get_annotations(cls, func).get("return", None) File "/Users/liujunyong/python310env/lib/python3.10/site-packages/tortoise/contrib/pydantic/utils.py", line 18, in get_annotations return typing.get_type_hints(method or cls, globalns=globalns) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 1856, in get_type_hints raise TypeError('{!r} is not a module, class, method, ' TypeError: <property object at 0x11e066110> is not a module, class, method, or function.

plusiv commented 1 year ago

up to now:

pydantic-core: 2.7 fix: https://github.com/pydantic/pydantic-core/pull/934

pydantic: last resource: with commit: https://github.com/pydantic/pydantic/commit/2575e71894a32dc615f9fa7a94ff37bb604a48a8

run result:

File "/Users/liujunyong/python310env/lib/python3.10/site-packages/tortoise/contrib/pydantic/creator.py", line 377, in pydantic_model_creator annotation = get_annotations(cls, func).get("return", None) File "/Users/liujunyong/python310env/lib/python3.10/site-packages/tortoise/contrib/pydantic/utils.py", line 18, in get_annotations return typing.get_type_hints(method or cls, globalns=globalns) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 1856, in get_type_hints raise TypeError('{!r} is not a module, class, method, ' TypeError: <property object at 0x11e066110> is not a module, class, method, or function.

Did you added a @property decorator to the computed method?