Skip to content

Commit

Permalink
⏩ Forward reference in type bound works.
Browse files Browse the repository at this point in the history
This particular pattern can be useful in node type declaration.
  • Loading branch information
rentruewang committed Jul 22, 2024
1 parent ec54fed commit 38fbf14
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/src/self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2024 RenChu Wang - All Rights Reserved

import abc
from collections.abc import Sequence
from typing import Protocol, TypeVar

T = TypeVar("T", bound="HasChildren", covariant=True)


class HasChildren(Protocol[T]):
@abc.abstractmethod
def children(self) -> Sequence[T]: ...


class HasSelfAsChildren(HasChildren["HasSelfAsChildren"]):
# It does autocomplete. Seems fine.
def children(self) -> Sequence["HasSelfAsChildren"]:
return []

0 comments on commit 38fbf14

Please sign in to comment.