3 ms·
Something like this does the trick: if TYPE_CHECKING: import WhateverClass https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING https://docs
by lopz 4y ago
Something like this does the trick:
if TYPE_CHECKING:
import WhateverClass
https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING https://docs.python.org/3/library/typing.html#typing.TYPE_CH...
- nomel 4y agoI’m a fan of having a single state, used for everything. Splitting the code up into two states, one for the linter and one for the execution, seems like a recipe for incorrectness and confusion. I would hate to refactor something like that.
- closed 4y agoThe issue is that sometimes a function can take a type that is an optional dependency, so you don't want to import it unless you are type checking. (And some types are defined in the typeshed so only exist to be imported during type checking; eg the type checker lib itself is a dependency in this case)
- deleted 4y ago[deleted]
- int_19h 4y agoIt's hardly elegant, though.
- Spivak 4y agoYeah if you’re like just put what is essentially if False: import blah unironically as good design and more than a necessary evil until a long-term solution emerges then we’ve jumped the shark.