pg.compound

Accessible via pg.compound, pg.symbolic.compound.

compound(base_class=None, args=None, **kwargs)[source]

Function decorator to create compound class.

Example:

@dataclasses.dataclass
class Foo:
  x: int
  y: int

  def sum(self):
    return self.x + self.y

@pg.compound
def foo_with_equal_x_y(v: int) -> Foo:
  return Foo(v, v)

f = foo_with_equal_x_y(1)

# First of all, the objects of compound classes can be used as an in-place
# replacement for the objects of regular classes, as they are subclasses of
# the regular classes.
assert issubclass(foo_with_equal_x_y, Foo)
assert isinstance(f, Foo)

# We can access symbolic attributes of the compound object.
assert f.v == 1

# We can also access the public APIs of the decomposed object.
assert f.x == 1
assert f.y == 1
assert f.sum() == 2

# Or explicit access the decomposed object.
assert f.decomposed == Foo(1, 1)

# Moreover, symbolic power is fully unleashed to the compound class.
f.rebind(v=2)
assert f.x == 2
assert f.y == 2
assert f.sum() == 4

# Err with runtime type check: 2.5 is not an integer.
f.rebind(v=2.5)
Parameters:
  • base_class – The base class of the compond class, which should be a pg.Object type. If None, it will be infererenced from the return annotation of factory_fn. If the annotation is not present or auto_typing is set to False, base_class must be present.

  • args – Symbolic args specification. See pg.compound_class for details.

  • **kwargs – Keyword arguments. See pg.compound_class for details.

Returns:

A symbolic compound class that subclasses base_class.