props
props
#
check_prop — a component prop checked against its annotation when the component is built.
- A primitive annotation (
bool,int,float,str) admits exactly its Python type (fits_primitive); aLiteraladmits exactly the values it lists;JSONValueadmits anything JSON-native (json_native);objectadmits anything. - A union keeps the first member that fits (
first_fitting); a list, tuple or dict checks each item against its type argument (type_argument), leaving aMissingitem out; acgscalar is coerced by its owncoerce; a container, struct, component or any other class is accepted by instance (a dict or list becomes the annotatedMaporArray). - Every failure names the prop's
positionand the annotation (annotation_name), so a mistake is found by name, not by guessing.
PRIMITIVES = (bool, int, float, str)
module-attribute
#
The annotations a value must match by Python type.
annotation_name(annotation)
#
annotation as it reads in an error message.
Source code in capturegraph-lib/capturegraph/ui/props.py
check_prop(value, annotation, position)
#
value as the prop annotation admits it at position, coerced where a scalar is.
assert check_prop([1, 2], list[int], "Stat.values") == [1, 2]
assert check_prop("photo.jpeg", cg.Image, "ImageViewer.image") == cg.Image("photo.jpeg")
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in capturegraph-lib/capturegraph/ui/props.py
first_fitting(annotation, position, attempt)
#
attempt on each member of the union annotation, returning the first that fits.
Raises:
| Type | Description |
|---|---|
TypeError
|
If no member fits; the message lists each member's reason. |
Source code in capturegraph-lib/capturegraph/ui/props.py
fits_primitive(value, annotation)
#
Whether value is exactly the primitive annotation names (bool is not int).
Source code in capturegraph-lib/capturegraph/ui/props.py
json_native(value, position)
#
value unchanged if it is JSON-native through and through.
Raises:
| Type | Description |
|---|---|
TypeError
|
If a nested value is not JSON-native, or an object key is not a string. |
Source code in capturegraph-lib/capturegraph/ui/props.py
text_key(key, position)
#
key as a dict prop's key.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in capturegraph-lib/capturegraph/ui/props.py
type_argument(annotation, index)
#
The index-th type argument of a generic alias, object when unparameterized.