Skip to content

Commit

Permalink
Introduce SimpleStructureHook
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Nov 7, 2024
1 parent ea30af6 commit 53e3049
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
28 changes: 15 additions & 13 deletions src/cattrs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,34 @@
StructureHandlerNotFoundError,
)
from .gen import override
from .types import SimpleStructureHook
from .v import transform_error

__all__ = [
"structure",
"unstructure",
"get_structure_hook",
"get_unstructure_hook",
"register_structure_hook_func",
"register_structure_hook",
"register_unstructure_hook_func",
"register_unstructure_hook",
"structure_attrs_fromdict",
"structure_attrs_fromtuple",
"global_converter",
"BaseConverter",
"Converter",
"AttributeValidationNote",
"BaseConverter",
"BaseValidationError",
"ClassValidationError",
"Converter",
"ForbiddenExtraKeysError",
"GenConverter",
"get_structure_hook",
"get_unstructure_hook",
"global_converter",
"IterableValidationError",
"IterableValidationNote",
"override",
"register_structure_hook_func",
"register_structure_hook",
"register_unstructure_hook_func",
"register_unstructure_hook",
"SimpleStructureHook",
"structure_attrs_fromdict",
"structure_attrs_fromtuple",
"structure",
"StructureHandlerNotFoundError",
"transform_error",
"unstructure",
"UnstructureStrategy",
]

Expand Down
8 changes: 4 additions & 4 deletions src/cattrs/gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
StructureHandlerNotFoundError,
)
from ..fns import identity
from ..types import SimpleStructureHook
from ._consts import AttributeOverride, already_generating, neutral
from ._generics import generate_mapping
from ._lc import generate_unique_filename
Expand Down Expand Up @@ -892,8 +893,6 @@ def mapping_unstructure_factory(

make_mapping_unstructure_fn: Final = mapping_unstructure_factory

MappingStructureFn = Callable[[Mapping[Any, Any], Any], T]


# This factory is here for backwards compatibility and circular imports.
def mapping_structure_factory(
Expand All @@ -903,7 +902,7 @@ def mapping_structure_factory(
key_type=NOTHING,
val_type=NOTHING,
detailed_validation: bool | Literal["from_converter"] = "from_converter",
) -> MappingStructureFn[T]:
) -> SimpleStructureHook[Mapping[Any, Any], T]:
"""Generate a specialized structure function for a mapping."""
fn_name = "structure_mapping"

Expand Down Expand Up @@ -1010,7 +1009,8 @@ def mapping_structure_factory(
for k, v in internal_arg_parts.items():
globs[k] = v

def_line = f"def {fn_name}(mapping, _{internal_arg_line}):"
globs["cl"] = cl
def_line = f"def {fn_name}(mapping, cl=cl{internal_arg_line}):"
total_lines = [def_line, *lines, " return res"]
script = "\n".join(total_lines)

Expand Down
13 changes: 13 additions & 0 deletions src/cattrs/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Protocol, TypeVar

In = TypeVar("In")
T = TypeVar("T")

__all__ = ["SimpleStructureHook"]


class SimpleStructureHook(Protocol[In, T]):
"""A structure hook with an optional (ignored) second argument."""

def __call__(self, _: In, /, cl=...) -> T:
...

0 comments on commit 53e3049

Please sign in to comment.