pg.typing.Union¶
Accessible via pg.typing.Union.
- class Union(candidates, default=MISSING_VALUE, is_noneable=False, frozen=False)[source]¶
Bases:
pg.typing.Generic,ValueSpecBaseValue 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:
Returns candidate types of this union spec.
Returns forward references used in this spec.
Returns acceptable value type(s) for current value spec.
Methods:
format([compact, verbose, root_indent])Format this object.
get_candidate(dest_spec)Get candidate by a destination value spec.
is_compatible(other)Union specific compatibility check.
noneable([is_noneable, use_none_as_default])Customized noneable for Union.
to_json(**kwargs)Returns a plain Python value as a representation for this object.
- format(compact=False, verbose=True, root_indent=0, **kwargs)[source]¶
Format this object.
- Return type:
- 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:
- 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.
- noneable(is_noneable=True, use_none_as_default=True)[source]¶
Customized noneable for Union.
- Return type:
- 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,tuplewhere the container types should have plain Python values as their values.