tortoise / tortoise-orm

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

Improve type hints for fastapi example #1601

Closed waketzheng closed 1 month ago

waketzheng commented 1 month ago

Fix mypy complaint for class that is dynamic created by pydantic_model_creator

Description

  1. After remove the module = ["examples.*"] section in pyproject.toml and then run mypy main.py in examples/fastapi/, it raises:
    main.py:36: error: Variable "models.User_Pydantic" is not valid as a type  [valid-type]
    main.py:36: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
    main.py:42: error: Variable "models.UserIn_Pydantic" is not valid as a type  [valid-type]
    main.py:42: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
    main.py:43: error: UserIn_Pydantic? has no attribute "model_dump"  [attr-defined]
    main.py:53: error: Variable "models.UserIn_Pydantic" is not valid as a type  [valid-type]
    main.py:53: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
    main.py:54: error: UserIn_Pydantic? has no attribute "model_dump"  [attr-defined]
    Found 5 errors in 1 file (checked 1 source file)
  2. cd examples/fastapi && pytest _tests.py show warning message with newer version of httpx:
    site-packages/httpx/_client.py:1426: DeprecationWarning: The 'app' shortcut is now deprecated. Use the explicit style 'transport=ASGITransport(app=...)' instead.
    warnings.warn(message, DeprecationWarning)

Motivation and Context

Our team have some projects tha using fastapi+tortoise-orm+mypy. It cost me a lot of time to find a pythonic way to fix mypy complaint about Xxx_Pydantic, finally this comment https://github.com/pydantic/pydantic/issues/615#issuecomment-504618897 give me the answer.

How Has This Been Tested?

make ci

Checklist:

coveralls commented 1 month ago

Pull Request Test Coverage Report for Build 9030535545

Details


Totals Coverage Status
Change from base Build 9028420328: 0.003%
Covered Lines: 5772
Relevant Lines: 6468

đź’› - Coveralls