Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type hints for fastapi example #1601

Merged
merged 4 commits into from May 14, 2024

Conversation

waketzheng
Copy link
Contributor

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)
  1. 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 pydantic/pydantic#615 (comment) give me the answer.

How Has This Been Tested?

make ci

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

coveralls commented May 3, 2024

Pull Request Test Coverage Report for Build 9030535545

Details

  • 6 of 6 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 87.935%

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

💛 - Coveralls

@abondar abondar merged commit b6680f1 into tortoise:develop May 14, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants