Skip to content

Commit

Permalink
feat: Defining the unmarshal API.
Browse files Browse the repository at this point in the history
  • Loading branch information
seandstewart committed Jun 19, 2024
1 parent 32827eb commit 8117e0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ extend-select = [

[tool.ruff.lint.per-file-ignores]
# Ignore `E402` (import violations) in all `__init__.py` files
"__init__.py" = ["E402"]
"__init__.py" = ["E402", "F403"]

[tool.ruff.lint.isort]
extra-standard-library = ["typing_extensions", "graphlib"]
Expand Down
2 changes: 2 additions & 0 deletions src/typelib/unmarshal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from typelib.unmarshal.factory import *
from typelib.unmarshal.routines import *
20 changes: 20 additions & 0 deletions src/typelib/unmarshal/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@

T = tp.TypeVar("T")

__all__ = (
"unmarshal",
"unmarshaller",
"DelayedUnmarshaller",
"NoOpUnmarshaller",
)


def unmarshal(typ: type[T] | refs.ForwardRef | str, value: tp.Any) -> T:
"""Unmarshal :py:param:`value` into :py:param:`typ`."""
routine = unmarshaller(typ)
unmarshalled = routine(value)
return unmarshalled


@compat.cache
def unmarshaller(
typ: type[T] | refs.ForwardRef | str,
) -> routines.AbstractUnmarshaller[T]:
"""Get an un-marshaller routine for a given type.
Args:
typ: The type annotation to generate an unmarshaller for.
May be a type, a :py:class:`typing.ForwardRef`, or string reference.
"""
nodes = graph.static_order(typ)
context: dict[type, routines.AbstractUnmarshaller] = {}
if not nodes:
Expand Down

0 comments on commit 8117e0c

Please sign in to comment.