|
14 | 14 | import uuid |
15 | 15 | import warnings |
16 | 16 |
|
17 | | -from typelib import graph, serdes |
| 17 | +from typelib import ctx, serdes |
18 | 18 | from typelib.py import compat, inspection, refs |
19 | 19 |
|
20 | 20 | T = tp.TypeVar("T") |
@@ -93,7 +93,7 @@ def __init__(self, t: type[T], context: ContextT, *, var: str | None = None): |
93 | 93 | def __call__(self, val: T) -> serdes.MarshalledValueT: ... |
94 | 94 |
|
95 | 95 |
|
96 | | -ContextT: tp.TypeAlias = "tp.Mapping[type | graph.TypeNode, AbstractMarshaller]" |
| 96 | +ContextT: tp.TypeAlias = "ctx.TypeContext[AbstractMarshaller]" |
97 | 97 |
|
98 | 98 |
|
99 | 99 | class NoOpMarshaller(AbstractMarshaller[T], tp.Generic[T]): |
@@ -463,25 +463,21 @@ def __init__(self, t: type[_ST], context: ContextT, *, var: str | None = None): |
463 | 463 |
|
464 | 464 | def _fields_by_var(self): |
465 | 465 | fields_by_var = {} |
466 | | - tp_var_map = {(t.type, t.var): m for t, m in self.context.items()} |
467 | 466 | hints = inspection.cached_type_hints(self.t) |
468 | 467 | for name, hint in hints.items(): |
469 | 468 | resolved = refs.evaluate(hint) |
470 | | - fkey = (hint, name) |
471 | | - rkey = (resolved, name) |
472 | | - if fkey in tp_var_map: |
473 | | - fields_by_var[name] = tp_var_map[fkey] |
474 | | - continue |
475 | | - if rkey in tp_var_map: |
476 | | - fields_by_var[name] = tp_var_map[rkey] |
| 469 | + m = self.context.get(hint) or self.context.get(resolved) |
| 470 | + if m is None: |
| 471 | + warnings.warn( |
| 472 | + "Failed to identify an unmarshaller for the associated type-variable pair: " |
| 473 | + f"Original ref: {hint}, Resolved ref: {resolved}. Will default to no-op.", |
| 474 | + stacklevel=4, |
| 475 | + ) |
| 476 | + fields_by_var[name] = NoOpMarshaller(hint, self.context, var=name) |
477 | 477 | continue |
478 | 478 |
|
479 | | - warnings.warn( # pragma: no cover |
480 | | - "Failed to identify an unmarshaller for the associated type-variable pair: " |
481 | | - f"Original ref: {fkey}, Resolved ref: {resolved}. Will default to no-op.", |
482 | | - stacklevel=3, |
483 | | - ) |
484 | | - fields_by_var[name] = NoOpMarshaller(hint, self.context, var=name) |
| 479 | + fields_by_var[name] = m |
| 480 | + |
485 | 481 | return fields_by_var |
486 | 482 |
|
487 | 483 | def __call__(self, val: _ST) -> MarshalledMappingT: |
|
0 commit comments