zlacker

[parent] [thread] 5 comments
1. welder+(OP)[view] [source] 2022-09-06 00:35:58
It does have some typehints... but I don't like Python types when they require importing the object only for the purpose of types. I only import objects when they're actually used in the non-type code, and I don't like to use `if typing.TYPE_CHECKING`.

These days I use Go when I need types.

replies(1): >>ttymck+U8
2. ttymck+U8[view] [source] 2022-09-06 01:50:29
>>welder+(OP)
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?
replies(1): >>welder+Mu
◧◩
3. welder+Mu[view] [source] [discussion] 2022-09-06 05:03:34
>>ttymck+U8
Yes, 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-hin...

replies(1): >>heaven+oi1
◧◩◪
4. heaven+oi1[view] [source] [discussion] 2022-09-06 12:23:55
>>welder+Mu
It happens very frequently if you are not very good at separating your data model from logic.
replies(1): >>welder+gp1
◧◩◪◨
5. welder+gp1[view] [source] [discussion] 2022-09-06 13:07:18
>>heaven+oi1
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?

Your comment makes it seem like you haven't experienced Python types enough, or you wouldn't think it was so easy.

replies(1): >>heaven+Pik
◧◩◪◨⬒
6. heaven+Pik[view] [source] [discussion] 2022-09-12 12:37:01
>>welder+gp1
> 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.

[go to top]