9
When to use typing.TYPE_CHECKING
(feddit.de)
Welcome to the Python community on the programming.dev Lemmy instance!
Past
November 2023
October 2023
July 2023
August 2023
September 2023
I only use it to avoid circular imports. Otherwise, I can import the type plainly.
Same, I think it's more common only to use when necessary.
The main case I can think of to use it more is for performance to save an import at runtime, but I don't think that's really valid, especially since the fact you're using the type annotation suggest the module would have been used elsewhere anyway so the import would be cached.
The argument against using anywhere is that it could be misleading as your editor may indicate that the import is definited even if it wouldn't be at runtime. Not sure if things like pylance have special handling to avoid this, would have to check...