Fields Implementation

Field implementations extending attrs.field().

Enums

enum disnake_compass.fields.FieldMetadata(value)[source]

Bases: Enum

Enum containing keys for field metadata.

PARSER = 1

Metadata key to store parser information.

FIELDTYPE = 2

Metadata key to store field type information. See FieldType.

flag disnake_compass.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().

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=True set.

Parameters:
  • default (_T | Literal[NOTHING]) – The default value for this field. The type of the default should match that of the type annotation.

  • parser (Parser[_T] | None) – The parser to use for converting this field to and from a string. The type of the parser should match that of the type annotation.

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 FieldType of 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 FieldType set.

Returns:

The type of the provided field.

Return type:

FieldType

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 of disnake_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=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]

disnake_compass.fields.is_field_of_type(field, kind)[source]

Check whether or not a field is marked as the provided FieldType.

Parameters:
  • field – The field to check.

  • kind – The FieldType to check for.

Returns:

Whether the provided field was of the provided FieldType.

Return type:

bool