Fix deflator and typename item types escaping
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "megasniff"
|
||||
version = "0.2.4.post1"
|
||||
version = "0.2.5"
|
||||
description = "Library for in-time codegened type validation"
|
||||
authors = [
|
||||
{ name = "nikto_b", email = "niktob560@yandex.ru" }
|
||||
|
||||
@@ -99,14 +99,12 @@ def _flatten_type(t: type | TypeAliasType) -> tuple[type, Optional[str]]:
|
||||
|
||||
def _schema_to_deflator_func(t: type | TypeAliasType) -> str:
|
||||
t, _ = _flatten_type(t)
|
||||
ret = 'deflate_' + typename(t).replace('.', '_')
|
||||
ret = ret.replace(' | ', '__OR__')
|
||||
ret = ret.replace('<', '')
|
||||
ret = ret.replace('>', '')
|
||||
ret = ret.replace(' ', '__')
|
||||
ret = ret.replace("'", '')
|
||||
ret = ret.replace('.', '_')
|
||||
return ret
|
||||
return ('deflate_' + typename(t)
|
||||
.replace('.', '_')
|
||||
.replace('[', '_of_')
|
||||
.replace(']', '_of_')
|
||||
.replace(',', '_and_')
|
||||
.replace(' ', '_'))
|
||||
|
||||
|
||||
def _fallback_unwrapper(obj: Any) -> JsonObject:
|
||||
@@ -159,7 +157,7 @@ class SchemaDeflatorGenerator:
|
||||
self.object_template = self.templateEnv.get_template(object_template_filename)
|
||||
|
||||
def schema_to_deflator(self,
|
||||
schema: type | UnionType,
|
||||
schema: type,
|
||||
strict_mode_override: Optional[bool] = None,
|
||||
explicit_casts_override: Optional[bool] = None,
|
||||
) -> Callable[[Any], dict[str, Any]]:
|
||||
@@ -170,15 +168,13 @@ class SchemaDeflatorGenerator:
|
||||
imports = ('from typing import Any\n'
|
||||
'from megasniff.exceptions import MissingFieldException, FieldValidationException\n')
|
||||
txt = imports + '\n' + txt
|
||||
print(txt)
|
||||
exec(txt, namespace)
|
||||
fn = namespace[_schema_to_deflator_func(schema)]
|
||||
if self._store_sources:
|
||||
setattr(fn, '__megasniff_sources__', txt)
|
||||
return fn
|
||||
|
||||
def schema_to_unwrapper(self, schema: type | UnionType | TypeAliasType, *,
|
||||
_visited_types: Optional[list[type]] = None):
|
||||
def schema_to_unwrapper(self, schema: type | TypeAliasType, *, _visited_types: Optional[list[type]] = None):
|
||||
if _visited_types is None:
|
||||
_visited_types = []
|
||||
else:
|
||||
@@ -253,16 +249,17 @@ class SchemaDeflatorGenerator:
|
||||
|
||||
ret_unw = ObjectUnwrapping(fields)
|
||||
else:
|
||||
raise NotImplementedError()
|
||||
raise NotImplementedError(f'type not implemented yet: {schema}')
|
||||
|
||||
return ret_unw, field_rename, set(_visited_types) | ongoing_types, recurcive_types
|
||||
|
||||
def _schema_to_deflator(self,
|
||||
schema: type | UnionType,
|
||||
schema: type | Sequence[TupleSchemaItem | tuple[str, type]] | OrderedDict[str, type],
|
||||
strict_mode_override: Optional[bool] = None,
|
||||
explicit_casts_override: Optional[bool] = None,
|
||||
into_type_override: Optional[type | TypeAliasType] = None,
|
||||
*,
|
||||
_funcname='deflate',
|
||||
_namespace=None,
|
||||
) -> tuple[str, dict]:
|
||||
if strict_mode_override is not None:
|
||||
|
||||
@@ -72,26 +72,21 @@ def typename(tp: type) -> str:
|
||||
if get_origin(tp) is None and hasattr(tp, '__name__'):
|
||||
ret = tp.__name__
|
||||
ret = str(tp)
|
||||
|
||||
ret = ret.replace(' | ', '__OR__')
|
||||
ret = ret.replace('<', '')
|
||||
ret = ret.replace('>', '')
|
||||
ret = ret.replace(' ', '__')
|
||||
ret = ret.replace("'", '')
|
||||
ret = ret.replace('.', '_')
|
||||
ret = (ret
|
||||
.replace('.', '_')
|
||||
.replace('[', '_of_')
|
||||
.replace(']', '_of_')
|
||||
.replace(',', '_and_')
|
||||
.replace(' ', '_')
|
||||
.replace('\'', '')
|
||||
.replace('<', '')
|
||||
.replace('>', ''))
|
||||
return ret
|
||||
|
||||
|
||||
def is_class_definition(obj):
|
||||
return isinstance(obj, type) or inspect.isclass(obj)
|
||||
return (isinstance(obj, type) or inspect.isclass(obj))
|
||||
|
||||
|
||||
def hashname(obj) -> str:
|
||||
ret = '_' + str(hash(obj)).replace('-', '_')
|
||||
ret = ret.replace(' | ', '__OR__')
|
||||
ret = ret.replace('<', '')
|
||||
ret = ret.replace('>', '')
|
||||
ret = ret.replace(' ', '__')
|
||||
ret = ret.replace("'", '')
|
||||
ret = ret.replace('.', '_')
|
||||
return ret
|
||||
return '_' + str(hash(obj)).replace('-', '_')
|
||||
|
||||
Reference in New Issue
Block a user