pg.symbolic.update_schema

Accessible via pg.symbolic.update_schema.

update_schema(cls, fields, extend=True, *, init_arg_list=None, metadata=None, description=None, serialization_key=None, additional_keys=None, add_to_registry=True)[source]

Updates the schema for a pg.Object subclass.

This function allows the user to update the symbolic fields associated with a symbolic class. It was intended to support meta-programming scenarios in which symbolic fields are dynamically generated.

Example:

class A(pg.Object):
  pass

# Add symbolic field 'x' to class A.
pg.symbolic.update_schema(A, [
  ('x', schema.Int())
])

# B inherits the symbolic field 'x' from A.
class B(A):
  pass

# Wipe out the symbolic field 'x' from B.
pg.symbolic.update_schema(B, [], extend=False)

See also: pg.members, pg.functor and pg.symbolize.

Return type:

None

Parameters:
  • cls – A symbolic Object subclass.

  • fields – A list of pg.typing.Field or equivalent tuple representation as (<key>, <value-spec>, [description], [metadata-objects]). key should be a string. value-spec should be pg_typing.ValueSpec classes or equivalent, e.g. primitive values which will be converted to ValueSpec implementation according to its type and used as its default value. description is optional only when field overrides a field from its parent class. metadata-objects is an optional list of any type, which can be used to generate code according to the schema.

  • extend – If True, extend existing schema using fields. Otherwise replace existing schema with a new schema created from fields.

  • init_arg_list – An optional sequence of strings as the positional argument list for __init__. This is helpful when symbolic attributes are inherited from base classes or the user want to change its order. If not provided, the init_arg_list will be automatically generated from symbolic attributes defined from pg.members in their declaration order, from the base classes to the subclass.

  • metadata – Optional dict of user objects as class-level metadata which will be attached to class schema.

  • description – An optional description to set.

  • 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.