From 5d7dd84c82ead1a7c81f1b7713ecf061ef0a892d Mon Sep 17 00:00:00 2001 From: voidZXL Date: Tue, 24 Dec 2024 21:36:19 +0800 Subject: [PATCH] add bound for class and function in parser field --- utype/__init__.py | 2 +- utype/parser/cls.py | 4 ++-- utype/parser/field.py | 8 +++++++- utype/parser/func.py | 2 +- utype/schema.py | 4 ++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/utype/__init__.py b/utype/__init__.py index ca132b9..2e8a008 100644 --- a/utype/__init__.py +++ b/utype/__init__.py @@ -12,7 +12,7 @@ register_transformer = TypeTransformer.registry.register -VERSION = (0, 6, 2) +VERSION = (0, 6, 3) def _get_version(): diff --git a/utype/parser/cls.py b/utype/parser/cls.py index f73af11..467cbdb 100644 --- a/utype/parser/cls.py +++ b/utype/parser/cls.py @@ -150,7 +150,7 @@ def generate_fields(self): **self.kwargs ) except Exception as e: - raise exc.ConfigError(f'{self.name}: parse field [{repr(key)}] failed with error: {e}') + raise exc.ConfigError(f'{self.name}: generate field [{repr(key)}] failed with error: {e}') fields.append(field) @@ -195,7 +195,7 @@ def generate_fields(self): **self.kwargs ) except Exception as e: - raise exc.ConfigError(f'{self.name}: parse field [{repr(key)}] failed with error: {e}') + raise exc.ConfigError(f'{self.name}: generate field [{repr(key)}] failed with error: {e}') fields.append(field) diff --git a/utype/parser/field.py b/utype/parser/field.py index e18e6b9..307d9a7 100644 --- a/utype/parser/field.py +++ b/utype/parser/field.py @@ -91,6 +91,9 @@ def __init__( max_contains: int = None, min_contains: int = None, unique_items: Union[bool, ConstraintMode] = None, + # todo? + # absence_error_message: str = None, + # parse_error_message: str = None, ): super().__init__(locals()) @@ -445,6 +448,7 @@ def __init__( final: bool = False, positional_only: bool = False, default=unprovided, # passed when using Annotated[] + bound=None, **kwargs ): @@ -458,6 +462,7 @@ def __init__( self.property = field_property self.final = final self.name = name + self.bound = bound all_aliases = [self.name] _aliases = [] @@ -1326,7 +1331,8 @@ def generate( final=final, positional_only=positional_only, default=_default, + bound=bound, **kwargs ) - parser_field.setup(options=options) + parser_field.setup(options) return parser_field diff --git a/utype/parser/func.py b/utype/parser/func.py index 5dfaaab..2f7bc41 100644 --- a/utype/parser/func.py +++ b/utype/parser/func.py @@ -430,7 +430,7 @@ def generate_fields(self): **self.kwargs ) except Exception as e: - raise exc.ConfigError(f'{self.name}: parse field [{repr(name)}] failed with error: {e}') + raise exc.ConfigError(f'{self.name}: generate field [{repr(name)}] failed with error: {e}') fields.append( field diff --git a/utype/schema.py b/utype/schema.py index 35f0050..b78c844 100644 --- a/utype/schema.py +++ b/utype/schema.py @@ -68,7 +68,7 @@ class DataClass(metaclass=LogicalMeta): __parser__: ClassParser __options__: Options - def __init__(self, **kwargs): + def __init__(self, *args, **kwargs): pass def __init_subclass__(cls, **kwargs): @@ -235,7 +235,7 @@ def __coerce_property__(self, field: ParserField, context: RuntimeContext): attr = field.property.fget(self) # get from the original getter except Exception as e: error_option = field.output_field.on_error if field.output_field else None - msg = f"@property: {repr(field.attname)} calculate failed with error: {e}" + msg = f"{self.__name__}: @property: {repr(field.attname)} calculate failed with error: {e}" if error_option == context.options.THROW: raise e.__class__(msg) from e else: