News for hackers. Stuff that matters.
Re:Why if TYPE_CHECKING? by diarrhea on Wednesday December 20, 2023 @06:21AM (#38707428)
One question comes to mind: isn't cyclic dependencies something to be resolved at the root cause (remove the cycle)? It's an issue that causes various pains down the line, like type checking in this case.
Re:Why if TYPE_CHECKING? by wzdd on Wednesday December 20, 2023 @06:35AM (#38707506)
The issue is that, often, without type annotations there would be no cyclic dependency (because any type matching the implicit interface would work). So by introducing type annotations (which means extra imports), you end up having to refactor quite a lot of code and the end game is a one-type-per-module-with-separate-interface-definition system of the sort encountered in, well, statically-typed languages. If you don't do this you end up with the 'Type in string' annotations mentioned in the article and TYPE_CHECKING-gated imports. Both of which feel kind of hackish.

It's fine to have implicit circular dependencies in Python because duck typing means that every function is effectively generic. Type annotations eliminate this and turn Python into a fundamentally different language.

Type annotations aren't just extra boilerplate in function signatures, they also have these sorts of knock-on effects.

Re:Why if TYPE_CHECKING? by gpderetta on Wednesday December 20, 2023 @08:22AM (#38708339)
Aside from type checking being a clusterfuck in python, wouldn't checking against a protocol be the right thing?
Re:Why if TYPE_CHECKING? by RhysU on Wednesday December 20, 2023 @07:09AM (#38707737)
I read 'Type in string' like a forward declaration. I find uses for them within a single module. They're not always hacks.
Re:Why if TYPE_CHECKING? by kkirsche on Wednesday December 20, 2023 @06:33AM (#38707499)
Broadly speaking, yes, but for some situations like database models with bi-directional relationships that can cause unnecessary maintenance burden simply for the benefit of type checking which this allows you to work around since the cyclical nature is only caused by typing not runtime semantics
Re:Why if TYPE_CHECKING? by m000 on Wednesday December 20, 2023 @07:30AM (#38707886)
Removing cycles may require extensive refactoring of the code, sometimes resulting in unintuitive layouts. All, just to solve the cyclic-import errors.

At the core of the problem is the fact that there's no thing as "partial import" in Python. When you do "from M import A" in Python, all the contents of M are evaluated and a reference to A is added to the current namespace. So, cyclic dependencies arise sooner or later, unless you adopt some very un-pythonic style for your codebase (e.g. one file per class).

Re:Why if TYPE_CHECKING? by maxcoder4 on Wednesday December 20, 2023 @06:37AM (#38707516)
In python it's very hard to avoid cyclic dependencies. Something as simple as a parent-child link between two classes is a cyclic dependency, and if the classes are in different files you have a car like in the blog post. This comes up especially hard when typing your codebase, because you have to name every type, and in python implementation is the interface, i.e. you can't just include a type definition file.
Re:Why if TYPE_CHECKING? by remram on Wednesday December 20, 2023 @07:01AM (#38707682)
> parent-child link between two classes is a cyclic dependency

What do you mean? Where is the cycle?

Re:Why if TYPE_CHECKING? by MereInterest on Wednesday December 20, 2023 @07:19AM (#38707814)
The parent holds a reference to the child. The child holds a reference to the parent.
Re:Why if TYPE_CHECKING? by remram on Wednesday December 20, 2023 @11:32AM (#38710447)
Why does the parent hold a reference to the child?

Are you talking about a specific pattern or implementation detail in the interpreter? Because I would not call this "dependency"...

Re:Why if TYPE_CHECKING? by diarrhea on Wednesday December 20, 2023 @07:16AM (#38707786)
I also wonder. If a child class knows its concrete parent (apart from where inheritance is declared), and not just references it via super(), things have already gone badly wrong.
Re:Why if TYPE_CHECKING? by coryrc on Wednesday December 20, 2023 @07:15AM (#38707785)

    class Parent:
      children : List[Child]

    class Child:
      __init__(self, parent: Parent):
Re:Why if TYPE_CHECKING? by remram on Wednesday December 20, 2023 @11:33AM (#38710473)
Ah yes, it might be what they mean. I assumed inheritance. Thanks.
Re:Why if TYPE_CHECKING? by gpderetta on Wednesday December 20, 2023 @09:38AM (#38709140)
Parametrize the parent with the child and the child with the parent. Tie the knot after both are defined.