From 36e343d3bc46e27a759703dd34ff15502aa2156a Mon Sep 17 00:00:00 2001 From: nikto_b Date: Wed, 20 Aug 2025 21:59:46 +0300 Subject: [PATCH] Make argnames escape --- pyproject.toml | 2 +- src/megasniff/inflator.py | 23 +++++++++++++++++++ src/megasniff/templates/inflator.jinja2 | 8 +++---- src/megasniff/templates/inflator_tuple.jinja2 | 10 ++++---- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f89a0d4..6033938 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "megasniff" -version = "0.2.3.post1" +version = "0.2.3.post2" description = "Library for in-time codegened type validation" authors = [ { name = "nikto_b", email = "niktob560@yandex.ru" } diff --git a/src/megasniff/inflator.py b/src/megasniff/inflator.py index 2aa7ae5..49fb6f6 100644 --- a/src/megasniff/inflator.py +++ b/src/megasniff/inflator.py @@ -32,15 +32,38 @@ class IterableTypeRenderData(TypeRenderData): is_union = False +def _escape_python_name(name: str) -> str: + name = name.replace('-', '__dash__').replace('+', '__plus__').replace('/', '__shash__') + if name[0].isnumeric(): + name = '__num__' + name + return name + + @dataclass class FieldRenderData: argname: str + argname_escaped: str constrs: TypeRenderData typename: str is_optional: bool allow_none: bool default_option: Optional[str] + def __init__(self, + argname: str, + constrs: TypeRenderData, + typename: str, + is_optional: bool, + allow_none: bool, + default_option: Optional[str]): + self.argname = argname + self.constrs = constrs + self.typename = typename + self.is_optional = is_optional + self.allow_none = allow_none + self.default_option = default_option + self.argname_escaped = _escape_python_name(argname) + class SchemaInflatorGenerator: templateLoader: jinja2.BaseLoader diff --git a/src/megasniff/templates/inflator.jinja2 b/src/megasniff/templates/inflator.jinja2 index 21351b8..43d6e9f 100644 --- a/src/megasniff/templates/inflator.jinja2 +++ b/src/megasniff/templates/inflator.jinja2 @@ -11,7 +11,7 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{ {% for conv in conversions %} if '{{conv.argname}}' not in from_data_keys: {% if conv.is_optional %} - {{conv.argname}} = {{conv.default_option}} + {{conv.argname_escaped}} = {{conv.default_option}} {% else %} raise MissingFieldException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}") {% endif %} @@ -21,12 +21,12 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{ {% if not conv.allow_none %} raise FieldValidationException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}", conv_data) {% else %} - {{conv.argname}} = None + {{conv.argname_escaped}} = None {% endif %} else: -{{ unwrap_type_data.render_segment(conv.argname, conv.constrs, "conv_data", false) | indent(4*3) }} +{{ unwrap_type_data.render_segment(conv.argname_escaped, conv.constrs, "conv_data", false) | indent(4*3) }} {% endfor %} - return {{funcname}}_tgt_type({% for conv in conversions %}{{conv.argname}}={{conv.argname}}, {% endfor %}) + return {{funcname}}_tgt_type({% for conv in conversions %}{{conv.argname_escaped}}={{conv.argname_escaped}}, {% endfor %}) diff --git a/src/megasniff/templates/inflator_tuple.jinja2 b/src/megasniff/templates/inflator_tuple.jinja2 index 57f9ac8..ec3d2f6 100644 --- a/src/megasniff/templates/inflator_tuple.jinja2 +++ b/src/megasniff/templates/inflator_tuple.jinja2 @@ -4,14 +4,14 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{from_type}}{% endif %}) {% if tgt_type is not none %} -> tuple {% endif %}: """ - {% for conv in conversions %}{{conv.argname}}:{{conv.typename}}, {% endfor %} + {% for conv in conversions %}{{conv.argname_escaped}}:{{conv.typename}}, {% endfor %} """ from_data_keys = from_data.keys() {% for conv in conversions %} if '{{conv.argname}}' not in from_data_keys: {% if conv.is_optional %} - {{conv.argname}} = {{conv.default_option}} + {{conv.argname_escaped}} = {{conv.default_option}} {% else %} raise MissingFieldException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}") {% endif %} @@ -21,12 +21,12 @@ def {{funcname}}(from_data: {% if from_type is none %}dict[str, Any]{% else %}{{ {% if not conv.allow_none %} raise FieldValidationException('{{conv.argname}}', "{{conv.typename | replace('"', "'")}}", conv_data) {% else %} - {{conv.argname}} = None + {{conv.argname_escaped}} = None {% endif %} else: -{{ unwrap_type_data.render_segment(conv.argname, conv.constrs, "conv_data", false) | indent(4*3) }} +{{ unwrap_type_data.render_segment(conv.argname_escaped, conv.constrs, "conv_data", false) | indent(4*3) }} {% endfor %} - return ({% for conv in conversions %}{{conv.argname}}, {% endfor %}) + return ({% for conv in conversions %}{{conv.argname_escaped}}, {% endfor %})