I think I'd prefer something like: ``` class Tree(Sealed[Node, Leaf]): ... class Node(Tree): ... class Leaf(Tree):. ```
Some languages do make you explicitly list out of the subclasses (e.g. Java and Ceylon) rather than requiring that all the subclasses simply be defined in the same file (e.g. Kotlin and Scala). I sympathize with both designs. The same-file sealed directive avoids having to repeat the names of the subclasses, while the explicit list avoids having the weird requirement that all the subclasses be in the same file. In a statically typed language, I think I would lean toward the explicit list, but in Python, this is very problematic. The explicit list is inherently a circular reference, which is fine in a statically typed language, but in Python there is just no place to put the explicit list where the runtime can pass over it. In your example, the interpreter would fail at Sealed[Node, Leaf] because name Node is not defined. Now that I think about it, maybe you could do some weird stuff with lazy annotations. There is no natural place to put the annotation, but there is no limit to the number of unnatural places. ``` form __future__ import annotations class Tree: ... __sealed__: Node | Leaf class Node(Tree): ... class Leaf(Tree): ... ```