Skip to content

Commit

Permalink
refactor: Remove type references from SerdeScope
Browse files Browse the repository at this point in the history
References to types used in generated functions are now added to
global scope of `exec` function. No need to have `types` property
in SerdeScope object anymore.
  • Loading branch information
yukinarit committed Nov 18, 2021
1 parent ebd6f39 commit bdd8784
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 43 deletions.
11 changes: 0 additions & 11 deletions serde/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ class SerdeScope:
defaults: Dict[str, Union[Callable, Any]] = field(default_factory=dict)
""" Default values of the dataclass fields (factories & normal values) """

types: Dict[str, Type] = field(default_factory=dict)
""" Type references to all used types within the dataclass """

code: Dict[str, str] = field(default_factory=dict)
""" Generated source code (only filled when debug is True) """

Expand Down Expand Up @@ -137,14 +134,6 @@ def __repr__(self) -> str:
res.append(f'{k}: {v}')
res.append('')

if self.types:
res.append('--------------------------------------------------')
res.append(self._justify('Type references in scope'))
res.append('--------------------------------------------------')
for k, v in self.types.items():
res.append(f'{k}: {v}')
res.append('')

if self.union_se_args:
res.append('--------------------------------------------------')
res.append(self._justify('Type list by used for union serialize functions'))
Expand Down
16 changes: 0 additions & 16 deletions serde/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ def wrap(cls: Type):
for typ in iter_types(cls):
if typ is cls or (is_primitive(typ) and not is_enum(typ)):
continue

scope.types[typename(typ)] = typ
g[typename(typ)] = typ

# render all union functions
Expand Down Expand Up @@ -661,11 +659,6 @@ def {{func}}(data, reuse_instances = {{serde_scope.reuse_instances_default}}):
if reuse_instances is Ellipsis:
reuse_instances = {{serde_scope.reuse_instances_default}}
{# List up all classes used by this class. -#}
{% for name in serde_scope.types|filter_scope -%}
{{name}} = serde_scope.types['{{name}}']
{% endfor -%}
if data is None:
return None
Expand All @@ -690,11 +683,6 @@ def {{func}}(data, reuse_instances = {{serde_scope.reuse_instances_default}}):
if reuse_instances is Ellipsis:
reuse_instances = {{serde_scope.reuse_instances_default}}
{# List up all classes used by this class. #}
{% for name in serde_scope.types|filter_scope %}
{{name}} = serde_scope.types['{{name}}']
{% endfor %}
if data is None:
return None
Expand All @@ -716,10 +704,6 @@ def {{func}}(data, reuse_instances = {{serde_scope.reuse_instances_default}}):
def render_union_func(cls: Type, union_args: List[Type]) -> str:
template = """
def {{func}}(data, reuse_instances):
{% for name in serde_scope.types|filter_scope %}
{{name}} = serde_scope.types['{{name}}']
{% endfor %}
# create fake dict so we can reuse the normal render function
fake_dict = {"fake_key":data}
Expand Down
16 changes: 0 additions & 16 deletions serde/se.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ def wrap(cls: Type):
for typ in iter_types(cls):
if typ is cls or (is_primitive(typ) and not is_enum(typ)):
continue

scope.types[typename(typ)] = typ
g[typename(typ)] = typ

# render all union functions
Expand Down Expand Up @@ -374,11 +372,6 @@ def {{func}}(obj, reuse_instances = {{serde_scope.reuse_instances_default}},
if not is_dataclass(obj):
return copy.deepcopy(obj)
{# List up all classes used by this class. #}
{% for name in serde_scope.types|filter_scope %}
{{name}} = serde_scope.types['{{name}}']
{% endfor %}
return (
{% for f in fields -%}
{% if not f.skip|default(False) %}
Expand Down Expand Up @@ -407,11 +400,6 @@ def {{func}}(obj, reuse_instances = {{serde_scope.reuse_instances_default}},
if not is_dataclass(obj):
return copy.deepcopy(obj)
{# List up all classes used by this class. #}
{% for name in serde_scope.types|filter_scope -%}
{{name}} = serde_scope.types['{{name}}']
{% endfor -%}
res = {}
{% for f in fields -%}
{% if not f.skip -%}
Expand Down Expand Up @@ -439,10 +427,6 @@ def {{func}}(obj, reuse_instances = {{serde_scope.reuse_instances_default}},
def render_union_func(cls: Type, union_args: List[Type]) -> str:
template = """
def {{func}}(obj, reuse_instances, convert_sets):
{% for name in serde_scope.types|filter_scope %}
{{name}} = serde_scope.types['{{name}}']
{% endfor %}
union_args = serde_scope.union_se_args['{{func}}']
{% for t in union_args %}
Expand Down

0 comments on commit bdd8784

Please sign in to comment.