Hacker News new | ask | show | jobs
by connorbrinton 1869 days ago
I also struggle with Mypy's strict mode, but I think recursive types are different from classes that can construct each other. Mypy doesn't have any problem with the following code

    from __future__ import annotations


    class X:
        def __init__(self, value: int) -> None:
            self.value = value

        def to_y(self) -> Y:
            return Y(self.value)


    class Y:
        def __init__(self, value: int) -> None:
            self.value = value

        def to_x(self) -> X:
            return X(self.value)


    x = X(10).to_y().to_x()
    print(x.value)
1 comments

Sorry, yes, that works. I should have been a little more explicit. This breaks down when Y is generic over X and X is generic over Y. IDK, I've had tons of problems with mutually recursive TypeVars, though my example was not of that - sorry.