#3731·ty

re-bind `Self@__init__` to the class

Author: danielpopescuCreated Jun 11, 2026Updated Sep 29, 2026
Labelsgenericsconstructor-callmethod-receiver

Summary

Type errors in function is_symetric. mypy or pyright are fine with the code.

python
from __future__ import annotations

from typing import Self


class BinaryTree[T]:
    def __init__(
        self,
        data: T,
        left: Self | None = None,
        right: Self | None = None,
    ) -> None:
        self.data = data
        self.left = left
        self.right = right



def is_symetric[T](bt: BinaryTree[T] | None) -> bool:
    def check_symetric(
        bt1: BinaryTree[T] | None,
        bt2: BinaryTree[T] | None,
    ) -> bool:
        match (bt1, bt2):
            case (None, None):
                return True
            case _ if not bt1 or not bt2:
                return False
            case _:
                assert bt1
                assert bt2
                return (
                    bt1.data == bt2.data
                    and check_symetric(bt1.left, bt2.right)
                    and check_symetric(bt1.right, bt2.left)
                )

    return check_symetric(bt.left, bt.right) if bt else False

Version

No response