Skip to content

brewing.generic

brewing.generic

1-line decorator to allow a class to be subclassed via generic syntax.

Specifically limited to the pattern where:

  1. a class variable is declared, unbound, as type[T], where T is a generic class parameter.
  2. An arbitary amount of such type parameters can be handled.

Example

from brewing.generic import runtime_generic


@runtime_generic
class SomeGenericClass[A, B]:
    attr_a: type[A]
    attr_b: type[B]


class ThingA:
    thinga = "foo"


class ThingB:
    thingb = "bar"


assert SomeGenericClass[ThingA, ThingB]().attr_a.thinga == "foo"
assert SomeGenericClass[ThingA, ThingB]().attr_b.thingb == "bar"

runtime_generic(cls)

Make some class cls able to be subclassed via generic, i.e. Foo[Bar] syntax.

Given a class Cls of type T, decorating with this will allow creation of a subclass Cls[T] with generic parameter T mapped to a matching unbound class attribute.