pg.typing.Dict

Accessible via pg.typing.Dict.

class Dict(schema=None, default=MISSING_VALUE, transform=None, is_noneable=False, frozen=False)[source]

Bases: pg.typing.Generic, ValueSpecBase

Value spec for dict type.

Examples:

# A required symbolic dict of arbitrary keys and values.
pg.typing.Dict()

# An optional Dict of keys started with 'foo' and int values, with None as
# the default value.
pg.typing.Dict([
    (pg.typing.StrKey(), pg.typing.Int())
]).noneable()

# A dict with two keys ('x' and 'y').
pg.typing.Dict([
    ('x', pg.typing.Float()),
    ('y', pg.typing.Int(min_value=1))
])

# A dict with a user validator.
def validate_sum(d):
  if sum(d.values()) > 1.:
    raise ValueError('The sum of the dict values should be less than 1.')

pg.typing.Dict([
    (pg.typing.StrKey(), pg.typing.Float())
], transform=validate_sum)

# A frozen dict that prevents subclass to extend/override.
pg.typing.Dict([
    ('x', 1),
    ('y, 2.0)
]).freeze()

Methods:

format([compact, verbose, root_indent, markdown])

Format this object.

noneable()

Override noneable in Dict to always set default value None.

set_default(default[, use_default_apply])

Set default value and returns self.

to_json(**kwargs)

Returns a plain Python value as a representation for this object.

Attributes:

forward_refs

Returns forward references used in this spec.

schema

Returns the schema of this dict spec.

format(compact=False, verbose=True, root_indent=0, *, markdown=False, **kwargs)[source]

Format this object.

Return type:

str

property forward_refs: Set[ForwardRef]

Returns forward references used in this spec.

noneable()[source]

Override noneable in Dict to always set default value None.

Return type:

pg.typing.Dict

property schema: Schema | None[source]

Returns the schema of this dict spec.

set_default(default, use_default_apply=True)[source]

Set default value and returns self.

Return type:

pg.ValueSpec

to_json(**kwargs)[source]

Returns a plain Python value as a representation for this object.

A plain Python value are basic python types that can be serialized into JSON, e.g: bool, int, float, str, dict (with string keys), list, tuple where the container types should have plain Python values as their values.

Return type:

Dict[str, Any]

Parameters:

**kwargs – Keyword arguments as flags to control JSON conversion.

Returns:

A plain Python value.