4 ms·
Why is this a problem? When would you reference the type but not actually need the type? If you're returning the type from some method in your API, but not inst
by ttymck 4y ago
Why is this a problem? When would you reference the type but not actually need the type? If you're returning the type from some method in your API, but not instantiating it there?
- welder 4y agoYes, if you're accepting the type as a param or returning the type. It happens very frequently, and without moving those imports behind "if typing.TYPE_CHECKING", you constantly run into cyclic imports. https://stackoverflow.com/questions/39740632/python-type-hinting-without-cyclic-imports https://stackoverflow.com/questions/39740632/python-type-hin...
- deleted 4y ago[deleted]
- heavenlyblue 4y agoIt happens very frequently if you are not very good at separating your data model from logic.
- welder 4y agoWhy would there exist a way in Python to conditionally import types, for the purpose of preventing cyclic imports, if cyclic imports weren't a problem? Your comment makes it seem like you haven't experienced Python types enough, or you wouldn't think it was so easy.
- heavenlyblue 4y ago> Your comment makes it seem like you haven't experienced Python types enough, or you wouldn't think it was so easy. Oh trust me I did and I constantly slap on the wrists juniors who over-complicate their solutions to the problem :) > Why would there exist a way in Python to conditionally import types, for the purpose of preventing cyclic imports, if cyclic imports weren't a problem? Because it's easier to understand than the solution to cyclic imports without conditional imports.