Fields Implementation¶
Field implementations extending attrs.field().
Enums¶
- enum disnake_compass.fields.FieldMetadata(value)[source]¶
Bases:
EnumEnum containing keys for field metadata.
- PARSER = 1¶
Metadata key to store parser information.
- flag disnake_compass.fields.FieldType(value)[source]¶
Bases:
FlagFlag 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().- META = 1¶
Internal field that facilitates disnake-compass functionality.
- INTERNAL = 2¶
Internal field that interfaces with disnake ui component fields.
- CUSTOM_ID = 4¶
Field parsed into/from the component’s custom id.
- SELECT = 8¶
Field parsed from a select component’s selected values.
- MODAL = 16¶
Field parsed from a modal component’s modal values.
- ALL = 31¶
Meta-value to facilitate checking for any field type.
Functions¶
- disnake_compass.fields.field(default=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=Trueset.- Parameters:
- Returns:
A new field with the provided default and/or parser.
- Return type:
Field[T]
- disnake_compass.fields.get_field_type(field, default=None)[source]¶
Get the
FieldTypeof the field.- Parameters:
field – The field of which to get the field type.
default – The default value to use if the field doesn’t have a
FieldTypeset.
- 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 ofdisnake_compass.field()to define a field.
- disnake_compass.fields.get_fields(cls, /, *, kind=<FieldType.ALL: 31>)[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.- Parameters:
cls – The class of which to get the fields.
kind – The kind(s) of fields to return. Can be any combination of
FieldTypes.
- disnake_compass.fields.get_parser(field)[source]¶
Get the user-provided parser of the provided field.
- Parameters:
field – The field for which to get the
disnake_compass.api.Parser.- Returns:
disnake_compass.api.Parser– The user-provided parser of the provided field.None– The field’s parser was automatically inferred.
- disnake_compass.fields.internal(default=NOTHING, *, 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=Trueset.- 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]