Parser API¶
Protocols for parser types.
Typing¶
- disnake_compass.api.parser.ParserType = ~ParserType¶
A typevar denoting the type of the parser.
A parser of a given type takes (any subclass of) that type as argument to
Parser.dumps(), and returns (any subclass of) that type fromParser.loads().
Classes¶
- class disnake_compass.api.parser.Parser(*args, **kwargs)[source]¶
Bases:
Protocol[ParserType]The baseline protocol for any kind of parser.
Any and all parser types must implement this protocol in order to be properly handled by disnake-compass.
Methods¶
- classmethod default(target_type, /)[source]¶
Return the default implementation of this parser type.
By default, this will just create the parser class with no arguments, but this can be overwritten on child classes for customised behaviour.
- Parameters:
target_type (
type[ParserType]) – The exact type that this parser should be created for- Returns:
The default parser instance for this parser type.
- Return type:
- await dumps(argument, /)[source]¶
Dump a value from a given type and convert it to a string.
In most cases it is imperative to ensure that this is done in a reversible way, such that calling
loads()on the result of this function returns the original input. For example:>>> parser = IntParser() >>> input_str = "1" >>> parsed_int = parser.loads(input_str) >>> dumped_int = parser.dumps(parsed_int) >>> input_str == dumped_int True
Any errors raised inside this method remain unmodified, and should be handled externally.
- Parameters:
argument (
ParserType) – The argument to parse into the desired type.- Returns:
- await loads(argument, /)[source]¶
Load a value from a string and apply the necessary conversion logic.
Any errors raised inside this method remain unmodified, and should be handled externally.
- Parameters:
argument (
str) – The argument to parse into the desired type.- Returns:
ParserType– In case the parser method was sync, the parsed result is returned as-is.Coroutine[ParserType] – In case the parser method was async, the parser naturally returns a coroutine. Awaiting this coroutine returns the parser result.