Fields Implementation#
Field implementations extending attrs.field()
.
Enums#
- enum components.fields.FieldMetadata(value)[source]#
Bases:
Enum
Enum containing keys for field metadata.
Valid values are as follows:
- PARSER = <FieldMetadata.PARSER: 1>#
- FIELDTYPE = <FieldMetadata.FIELDTYPE: 2>#
- flag components.fields.FieldType(value)[source]#
Bases:
Flag
Flag containing field metadata values for the field type.
Note that a field can only ever be one of these types. This is a flag for the sole reason of facilitating unions in lookups using
get_fields()
.Valid values are as follows:
- INTERNAL = <FieldType.INTERNAL: 1>#
- CUSTOM_ID = <FieldType.CUSTOM_ID: 2>#
- SELECT = <FieldType.SELECT: 4>#
- MODAL = <FieldType.MODAL: 8>#
The
Flag
and its members also have the following methods:- classmethod ALL()[source]#
Meta-value for all field types.
Mainly intended for use in
get_fields()
.
Functions#
- components.fields.field(default=_Nothing.NOTHING, *, parser=None)[source]#
Define a custom ID field for the component.
The type annotation for this field is used to parse incoming custom ids.
This is a wrapper around
attrs.field()
.Note
In most cases, simply using a typehint will suffice to define a field. This function is generally only needed if you wish to supply a default value or custom parser.
Note
Fields created this way always have
kw_only=True
set.- Parameters:
- Returns:
A new field with the provided default and/or parser.
- Return type:
Field
[T
]
- components.fields.get_field_type(field, default=None)[source]#
Get the
FieldType
of the field.- Parameters:
- Returns:
The type of the provided field.
- Return type:
- Raises:
TypeError – The provided field does not have a field type set, and no default was provided. The most common cause of this is using
attrs.field()
instead ofcomponents.field()
to define a field.
- components.fields.get_fields(cls, /, *, kind=FieldType.ALL())[source]#
Get the attributes of an attrs class.
This wraps
attrs.fields()
to be less strict typing-wise and has special handling for internal fields.
- components.fields.internal(default, *, frozen=False)[source]#
Declare a field as internal.
This is used internally to differentiate component parameters from user-defined custom id parameters.
This is a wrapper around
attrs.field()
.Note
Fields created this way always have
kw_only=True
set.- Parameters:
default (
_T
) – The default value for this field. The type of the default should match that of the type annotation.frozen (
bool
) – Whether or not the field should be marked frozen. A frozen field cannot be modified after the class has been created.
- Returns:
A new field with the provided default and frozen status.
- Return type:
Field
[T
]