pg.typing.Union

Accessible via pg.typing.Union.

class Union(candidates, default=MISSING_VALUE, is_noneable=False, frozen=False)[source]

Bases: pg.typing.Generic, ValueSpecBase

Value spec for Union.

Examples:

# A required int or float value.
pg.typing.Union([pg.typing.Int(), pg.typing.Float()])

# An optional int or float value with default set to None.
pg.typing.Union([pg.typing.Int(), pg.typing.Float()]).noneable()

# A dict of specific keys, instance of class A or B, with {x=1} as its
# default value.
pg.typing.Union([
    pg.typing.Dict([
        ('x', pg.typing.Int(min_value=1)),
    ]),
    pg.typing.Object(A),
    pg.typing.Object(B),
], default={'x': 1})

Attributes:

candidates

Returns candidate types of this union spec.

forward_refs

Returns forward references used in this spec.

value_type

Returns acceptable value type(s) for current value spec.

Methods:

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

Format this object.

get_candidate(dest_spec)

Get candidate by a destination value spec.

is_compatible(other)

Union specific compatibility check.

noneable()

Customized noneable for Union.

to_json(**kwargs)

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

property candidates: List[ValueSpec][source]

Returns candidate types of this union 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.

get_candidate(dest_spec)[source]

Get candidate by a destination value spec.

Return type:

Optional[pg.ValueSpec]

Parameters:

dest_spec – destination value spec which is a superset of the value spec to return. E.g. Any (dest_spec) is superset of Int (child spec).

Returns:

The first value spec under Union with which the destination value spec

is compatible.

is_compatible(other)[source]

Union specific compatibility check.

Return type:

bool

noneable()[source]

Customized noneable for Union.

Return type:

pg.typing.Union

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.

property value_type: Tuple[Type[Any]] | None

Returns acceptable value type(s) for current value spec.