From 7d947d377638f1cea45d0f2bed079e3fabdbf515 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 25 Sep 2025 18:23:44 -0400 Subject: [PATCH] Add `types`-mod to `.msg._exts.dec_type_union()` Such that decoded output equivalent to `str|None` can actually be unpacked from a `type_names = ['str', 'NoneType]` without just ignoring the null-type entry.. Previously, the loop would fall through silently ignoring the `None` -> `NoneType` string representation mapped by `.enc_type_union()` and the output union would be incorrect. Deats, - include the stdlib's `types` in the lookup loop, obvi changing the output var's name to `_types` to not collide. - add output checking versus input `type_names` such that we raise a value-error with a case specific `report: str` when either, * the output `_types: list[Type]` is empty, * the `len(_types) != len(type_names)`. --- tractor/msg/_exts.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/tractor/msg/_exts.py b/tractor/msg/_exts.py index 31eafb5d..12acd653 100644 --- a/tractor/msg/_exts.py +++ b/tractor/msg/_exts.py @@ -33,9 +33,7 @@ converters, |_ https://jcristharif.com/msgspec/changelog.html ''' -from types import ( - ModuleType, -) +import types import typing from typing import ( Type, @@ -44,35 +42,51 @@ from typing import ( def dec_type_union( type_names: list[str], - mods: list[ModuleType] = [] + mods: list[types.ModuleType] = [] ) -> Type|Union[Type]: ''' Look up types by name, compile into a list and then create and return a `typing.Union` from the full set. ''' - # import importlib - types: list[Type] = [] + _types: list[Type] = [] for type_name in type_names: for mod in [ typing, - # importlib.import_module(__name__), + types, ] + mods: if type_ref := getattr( mod, type_name, False, ): - types.append(type_ref) + _types.append(type_ref) + break - # special case handling only.. - # ipc_pld_spec: Union[Type] = eval( - # pld_spec_str, - # {}, # globals - # {'typing': typing}, # locals - # ) + report: str = '' + if not _types: + report: str = 'No type-instances could be resolved from `type_names` ??\n' - return Union[*types] + elif len(type_names) != len(_types): + report: str = ( + f'Some type-instances could not be resolved from `type_names` ??\n' + f'_types: {_types!r}\n' + ) + + if report: + raise ValueError( + report + + + f'type_names: {type_names!r}\n' + ) + + if not _types: + raise ValueError( + f'No type-instance could be resolved from `type_names` ??\n' + f'type_names: {type_names!r}\n' + ) + + return Union[*_types] def enc_type_union(