Enum Parser Implementation

Parser implementations for standard library and disnake enums and flags.

Classes

class disnake_compass.impl.parser.enum.EnumParser(enum_class, *, store_by_value=None)[source]

Bases: Parser[_EnumT]

Parser type for enums and flags.

Enums and flags are stored by value instead of by name. This makes parsing a bit slower, but values are generally shorter than names.

This parser type works for standard library and disnake enums and flags. Note that this only works for enums and flags where all values are of the same type.

Parameters:
  • enum_class (type[_EnumT]) – The enum or flag class to use for parsing.

  • store_by_value (bool | None) –

    Whether loads() and dumps() expect the enum value type or a string.

    For enum types where the members are not all of the same type, this must be False.

    For enum types where all members are integers, this defaults to True, otherwise this defaults to False.

Attributes

enum_class: type[_EnumT]

The enum or flag class to use for parsing.

store_by_value: bool

Whether loads() and dumps() expect the enum’s value type or a string.

For enum types where the members are not all of the same type, this must be False.

value_parser: Parser[Any]

The parser responsible for converting to/from the enum type.

If store_by_values is set to False, this is always a StringParser.

Methods

await dumps(argument)[source]

Dump an enum member into a string.

Note

If store_by_value is True, this dumps the name of the enum member passed as argument; otherwise, this dumps its value.

Parameters:

argument (_EnumT) – The value that is to be dumped.

await loads(argument, /)[source]

Load an enum member from a string.

This uses the underlying value_parser.

Note

If store_by_value is True, the argument is expected to be the value of an enum member; otherwise, the argument is expted to be the name.

Parameters:

argument (str) –

The value that is to be loaded into a channel.

This always matches the channel type of the parser.

disnake_compass.impl.parser.enum.FlagParser[source]

alias of EnumParser