pg.compound_class

Accessible via pg.compound_class, pg.symbolic.compound_class.

compound_class(factory_fn, base_class=None, args=None, *, lazy_build=True, auto_doc=True, auto_typing=True, serialization_key=None, additional_keys=None, add_to_registry=False)[source]

Creates a compound class from a factory function.

Return type:

Type[Compound]

Parameters:
  • factory_fn – A function that produces a compound object.

  • 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. args is a list of tuples, each describes an argument from the input function. Each tuple is the format of (<argumment-name>, <value-spec>, [description], [metadata-objects]). argument-name - a str or pg_typing.StrKey object. When pg_typing.StrKey is used, it describes the wildcard keyword argument. value-spec - a pg_typing.ValueSpec object or equivalent, e.g. primitive values which will be converted to ValueSpec implementation according to its type and used as its default value. description - a string to describe the agument. metadata-objects - an optional list of any type, which can be used to generate code according to the schema. There are notable rules in filling the args: 1) When args is None or arguments from the function signature are missing from it, schema.Field for these fields will be automatically generated and inserted into args. That being said, every arguments in input function will have a schema.Field counterpart in Functor.schema.fields sorted by the declaration order of each argument in the function signature ( other than the order in args). 2) Default argument values are specified along with function definition as regular python functions, instead of being set at schema.Field level. But validation rules can be set using args and apply to argument values.

  • lazy_build – If True, factory_fn will be called upon first use. Otherwise, it will be called at construction.

  • auto_doc – If True, the descriptions of argument fields will be inherited from factory_fn docstr if they are not explicitly specified through args.

  • auto_typing – If True, the value spec for constraining each argument will be inferred from its annotation. Otherwise the value specs for all arguments will be pg.typing.Any().

  • serialization_key – An optional string to be used as the serialization key for the class during sym_jsonify. If None, cls.__type_name__ will be used. This is introduced for scenarios when we want to relocate a class, before the downstream can recognize the new location, we need the class to serialize it using previous key.

  • additional_keys – An optional list of strings as additional keys to deserialize an object of the registered class. This can be useful when we need to relocate or rename the registered class while being able to load existing serialized JSON values.

  • add_to_registry – If True, the newly created functor class will be added to the registry for deserialization.

Returns:

A callable that converts a factory function into a subclass of the base

class.