full-stack-fastapi-template icon indicating copy to clipboard operation
full-stack-fastapi-template copied to clipboard

Why do we need import TYPE_CHECKING in models?

Open BEEugene opened this issue 4 years ago • 2 comments

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?

BEEugene avatar Jul 26 '21 17:07 BEEugene

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

haviduck avatar Jul 27 '21 02:07 haviduck

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.

raphant avatar Aug 08 '21 23:08 raphant