Spec Adapters¶
Adapters convert various input specifications into a normalized format for parsing.
Learn how to create custom adapters in the Advanced Usage guide.
The following built-in adapters are not meant to be used directly. They serve more as an example than anything else.
anyschema.adapters
¶
attrs_adapter(spec: AttrsClassType) -> FieldSpecIterable
¶
Adapter for attrs classes.
Extracts field information from an attrs class and converts it into an iterator
yielding field information as (field_name, field_type, metadata) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
AttrsClassType
|
An attrs class (not an instance). |
required |
Yields:
| Type | Description |
|---|---|
FieldSpecIterable
|
A tuple of |
Examples:
>>> from attrs import define
>>>
>>> @define
... class Student:
... name: str
... age: int
>>>
>>> list(attrs_adapter(Student))
[('name', <class 'str'>, ()), ('age', <class 'int'>, ())]
Source code in anyschema/adapters.py
dataclass_adapter(spec: DataclassType) -> FieldSpecIterable
¶
Adapter for dataclasses.
Converts a dataclass into an iterator yielding field information as
(field_name, field_type, metadata) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
DataclassType
|
A dataclass with annotated fields. |
required |
Yields:
| Type | Description |
|---|---|
FieldSpecIterable
|
A tuple of |
FieldSpecIterable
|
The metadata tuple is always empty |
Examples:
>>> from dataclasses import dataclass
>>>
>>> @dataclass
... class Student:
... name: str
... age: int
>>>
>>> list(dataclass_adapter(Student))
[('name', <class 'str'>, ()), ('age', <class 'int'>, ())]
Source code in anyschema/adapters.py
into_ordered_dict_adapter(spec: IntoOrderedDict) -> FieldSpecIterable
¶
Adapter for Python mappings and sequences of field definitions.
Converts a mapping (e.g., dict) or sequence of 2-tuples into an iterator yielding field information as
(field_name, field_type, metadata) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
IntoOrderedDict
|
A mapping from field names to types, or a sequence of |
required |
Yields:
| Type | Description |
|---|---|
FieldSpecIterable
|
A tuple of |
FieldSpecIterable
|
The metadata tuple is always empty |
Examples:
>>> list(into_ordered_dict_adapter({"name": str, "age": int}))
[('name', <class 'str'>, ()), ('age', <class 'int'>, ())]
>>> list(into_ordered_dict_adapter([("age", int), ("name", str)]))
[('age', <class 'int'>, ()), ('name', <class 'str'>, ())]
Source code in anyschema/adapters.py
pydantic_adapter(spec: type[BaseModel]) -> FieldSpecIterable
¶
Adapter for Pydantic BaseModel classes.
Extracts field information from a Pydantic model class and converts it into an iterator
yielding field information as (field_name, field_type, metadata) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
type[BaseModel]
|
A Pydantic |
required |
Yields:
| Type | Description |
|---|---|
FieldSpecIterable
|
A tuple of |
Examples:
>>> from pydantic import BaseModel, Field
>>> from typing import Annotated
>>>
>>> class Student(BaseModel):
... name: str
... age: Annotated[int, Field(ge=0)]
>>>
>>> list(pydantic_adapter(Student))
[('name', <class 'str'>, ()), ('age', ForwardRef('Annotated[int, Field(ge=0)]', is_class=True), ())]
Source code in anyschema/adapters.py
typed_dict_adapter(spec: TypedDictType) -> FieldSpecIterable
¶
Adapter for TypedDict classes.
Converts a TypedDict into an iterator yielding field information as
(field_name, field_type, metadata) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
TypedDictType
|
A TypedDict class (not an instance). |
required |
Yields:
| Type | Description |
|---|---|
FieldSpecIterable
|
A tuple of |
FieldSpecIterable
|
The metadata tuple is always empty |
Examples:
>>> from typing_extensions import TypedDict
>>>
>>> class Student(TypedDict):
... name: str
... age: int
>>>
>>> list(typed_dict_adapter(Student))
[('name', <class 'str'>, ()), ('age', <class 'int'>, ())]
Source code in anyschema/adapters.py
Adapters specification¶
Adapters must follow this signature:
from typing import Iterator, TypeAlias, Callable, Any, Generator
from anyschema.typing import FieldMetadata, FieldName, FieldType
FieldSpec: TypeAlias = tuple[FieldName, FieldType, FieldMetadata]
def my_custom_adapter(spec: Any) -> Iterator[FieldSpec]:
...
They don't need to be functions; any callable is acceptable.