9
When to use typing.TYPE_CHECKING
(feddit.de)
Welcome to the Python community on the programming.dev Lemmy instance!
October 2023
November 2023
Past
July 2023
August 2023
September 2023
I don’t like having to quote the types, so I use it exclusively for avoiding circular imports.
Thanks for the tip
Why not use
from __future__ import annotations
?You still need to import the type before using it in a stringified type annotation for it to be valid though, so you'd need the import in an
if TYPE_CHECKING:
block either way, no?Yes, but if it’s in a TYPE_CHECKING block I can ONLY use the annotation with quotes*, which is why I only use that method if I must.
from __future__ import annotations
as I’ve just learned.Ah yeah, I see what you meant.