fastapi / full-stack-fastapi-template

Full stack, modern web application template. Using FastAPI, React, SQLModel, PostgreSQL, Docker, GitHub Actions, automatic HTTPS and more.
MIT License
26.78k stars 4.73k forks source link

Why do we need import TYPE_CHECKING in models? #406

Open BEEugene opened 3 years ago

BEEugene commented 3 years ago

Hi!

This is a great template project. Why should we import type_checking in models? from typing import TYPE_CHECKING https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/490c554e23343eec0736b06e59b2108fdd057fdc/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/models/item.py

As far as I understood this import is needed for linter, but how it works? Does it somehow check database consistency?

haviduck commented 3 years ago

its for mypy, my guess is to lint relations while blocking with noqa

raphant commented 3 years ago

I think it's primarily for auto-completion while avoiding circular imports. In the link you provided, from .user import User # noqa: F401 will only import User for the sake of type checking. It will not execute any code from the .user module.