Skip to content

Commit

Permalink
feat: rename some inspections and rework imports
Browse files Browse the repository at this point in the history
  • Loading branch information
seandstewart committed Aug 4, 2024
1 parent 26fb243 commit 3a5946e
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 187 deletions.
18 changes: 12 additions & 6 deletions src/typelib/ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ class TypeContext(dict):
"""A key-value mapping which can map between forward references and real types."""

def __missing__(self, key: graph.TypeNode | type | refs.ForwardRef):
"""Hook to handle missing type references.
Allows for sharing lookup results between forward references and real types.
Args:
key: The type or reference.
"""
# Eager wrap in a TypeNode
if not isinstance(key, graph.TypeNode):
key = graph.TypeNode(type=key)
return self[key]

# If we missed a ForwardRef, we've already tried this, bail out.
type = key.type
if isinstance(type, refs.ForwardRef):
raise KeyError(key)
ref = (
key.type
if isinstance(type, refs.ForwardRef)
else refs.forwardref(
inspection.get_qualname(type), module=getattr(type, "__module__", None)
)

ref = refs.forwardref(
inspection.qualname(type), module=getattr(type, "__module__", None)
)
node = dataclasses.replace(key, type=ref)
return self[node]
4 changes: 2 additions & 2 deletions src/typelib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def get_type_graph(t: type) -> graphlib.TopologicalSorter[TypeNode]:
# wrap in a ForwardRef and don't add it to the stack
# This will terminate this edge to prevent infinite cycles.
if is_visited and can_be_cyclic:
qualname = inspection.get_qualname(child)
qualname = inspection.qualname(child)
*rest, refname = qualname.split(".", maxsplit=1)
is_argument = var is not None
module = getattr(child, "__module__", None)
Expand Down Expand Up @@ -178,7 +178,7 @@ class TypeNode:


def _level(t: typing.Any) -> typing.Iterable[tuple[str | None, type]]:
args = inspection.get_args(t)
args = inspection.args(t)
# Only pull annotations from the signature if this is a user-defined type.
is_structured = inspection.isstructuredtype(t)
members = inspection.get_type_hints(t, exhaustive=is_structured)
Expand Down
8 changes: 4 additions & 4 deletions src/typelib/interchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
... class Class:
... attr: str
...
>>> protocol = interchange.protocol(Class)
>>> protocol.unmarshal({"attr": "value"})
>>> proto = interchange.protocol(Class)
>>> proto.unmarshal({"attr": "value"})
Class(attr="value")
>>> protocol.codec.decode(b'{"attr": "value"}')
>>> proto.codec.decode(b'{"attr": "value"}')
Class(attr="value")
>>> protocol.codec.encode(Class(attr="value"))
>>> proto.codec.encode(Class(attr="value"))
b'{"attr":"value"}'
"""

Expand Down
10 changes: 5 additions & 5 deletions src/typelib/marshal/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __init__(self, t: type[LiteralT], context: ContextT, *, var: str | None = No
var: A variable name for the indicated type annotation (unused, optional).
"""
super().__init__(t, context, var=var)
self.values = inspection.get_args(t)
self.values = inspection.args(t)

def __call__(self, val: LiteralT) -> serdes.MarshalledValueT:
"""Enforce the given value is a member of the bound `Literal` type.
Expand Down Expand Up @@ -256,7 +256,7 @@ def __init__(self, t: type[UnionT], context: ContextT, *, var: str | None = None
var: A variable name for the indicated type annotation (unused, optional).
"""
super().__init__(t, context, var=var)
self.stack = inspection.get_args(t)
self.stack = inspection.args(t)
self.ordered_routines = [self.context[typ] for typ in self.stack]

def __call__(self, val: UnionT) -> serdes.MarshalledValueT:
Expand Down Expand Up @@ -329,7 +329,7 @@ def __init__(self, t: type[MappingT], context: ContextT, *, var: str | None = No
var: A variable name for the indicated type annotation (unused, optional).
"""
super().__init__(t, context, var=var)
key_t, value_t = inspection.get_args(t)
key_t, value_t = inspection.args(t)
self.keys = context[key_t]
self.values = context[value_t]

Expand Down Expand Up @@ -364,7 +364,7 @@ def __init__(
"""
super().__init__(t=t, context=context, var=var)
# supporting tuple[str, ...]
(value_t, *_) = inspection.get_args(t)
(value_t, *_) = inspection.args(t)
self.values = context[value_t]

def __call__(self, val: IterableT) -> MarshalledIterableT:
Expand Down Expand Up @@ -400,7 +400,7 @@ def __init__(
var: A variable name for the indicated type annotation (unused, optional).
"""
super().__init__(t, context, var=var)
self.stack = inspection.get_args(t)
self.stack = inspection.args(t)
self.ordered_routines = [self.context[vt] for vt in self.stack]

def __call__(self, val: compat.TupleT) -> MarshalledIterableT:
Expand Down
Loading

0 comments on commit 3a5946e

Please sign in to comment.