bagof.converters.base
Base class for all converters.
Attributes
ConverterRegistry
module-attribute
A registry of converters, mapping type hints to converter classes.
register_converter
module-attribute
Backward-compatible alias for Converter.register.
get_converter
module-attribute
Backward-compatible alias for Converter.get.
get_converter_class
module-attribute
Backward-compatible alias for Converter.get_class.
Classes
Converter
Bases: MagicHint[TO], Generic[TO, FROM]
Base class for magic converters.
Converters take a value and return a converted version of it. They are registered in a global registry and looked up by type hint.
Note
Failed conversions raise a ConversionError. Its concrete
subclasses -- ValueConversionError (also a ValueError)
and TypeConversionError (also a TypeError) -- inherit
the matching builtin, so callers that already catch ValueError
or TypeError still catch conversion failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
Any
|
The type hint to use for this magic object. If not provided, the default hint for the class is used. |
UNSET
|
compose
|
bool
|
Whether to compose this converter with others, when they are
found in |
False
|
Methods:
like
Return a type hint describing valid inputs for this converter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__reentrant
|
tuple
|
Used internally to avoid infinite recursion. |
()
|
Returns:
| Type | Description |
|---|---|
Any
|
A type hint for valid input values. |
__call__
Convert the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
FROM
|
The value to convert. |
required |
Returns:
| Type | Description |
|---|---|
TO
|
The converted value. |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If the value cannot be converted. |
error
error(value: Any, message: str | None = None, **kwargs: Any) -> ConversionError
Return a ConversionError with the given value and message.
type_error
type_error(value: Any, message: str | None = None) -> TypeConversionError
Return a TypeConversionError with the given value.
value_error
value_error(value: Any, message: str | None = None) -> ValueConversionError
Return a ValueConversionError with the given value.
register
staticmethod
register(converter: Type[Converter], *hints: Unpack[tuple[Any]], registry: ConverterRegistry = ...) -> Type[Converter]
register(*hints: Unpack[tuple[Any]], registry: ConverterRegistry = ...) -> ClassDecorator
Register a converter class for one or more type hints.
Can be used as a decorator or called directly:
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*hints
|
Any
|
One or more type hints to register the converter class for. |
()
|
registry
|
ConverterRegistry
|
The registry to register the converter class in. Defaults to the global registry. |
CONVERTERS
|
get
staticmethod
get(hint: Any, registry: ConverterRegistry = CONVERTERS, fallback: Type[Converter] | None = None) -> Converter | None
Get the best-matching converter for a given type hint.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
Any
|
The type hint for which to get a converter. |
required |
registry
|
ConverterRegistry
|
The registry to look up the converter in. Defaults to the global registry. |
CONVERTERS
|
fallback
|
Optional[Type[Converter]]
|
The fallback converter class to use if no matching converter is found. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Converter]
|
The best-matching converter for the given type hint, or |
get_class
staticmethod
get_class(hint: Any, registry: ConverterRegistry = CONVERTERS, fallback: Type[Converter] | None = None) -> Type[Converter] | None
Get the best-matching converter class for a given type hint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
Any
|
The type hint for which to get a converter. |
required |
registry
|
ConverterRegistry
|
The registry to look up the converter in. Defaults to the global registry. |
CONVERTERS
|
fallback
|
Optional[Type[Converter]]
|
The fallback converter class to use if no matching converter is found. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Type[Converter]]
|
The best-matching converter class for the given type hint,
or |
Functions:
wrap_converter
Wrap a converter so that it has the correct input and output annotations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converter
|
Converter
|
The converter to wrap. |
required |
TO
|
Any
|
The output type hint. Defaults to |
UNSET
|
FROM
|
Any
|
The input type hint. Defaults to |
UNSET
|
Returns:
| Type | Description |
|---|---|
Callable
|
A callable that wraps |