Skip to content

Commit

Permalink
Remove extra call if it's call expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Nov 15, 2023
1 parent 66265ab commit 8597bd9
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions mashumaro/codecs/_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import is_dataclass
import re
from typing import Any, Callable, Optional, Type

from mashumaro.core.meta.code.builder import CodeBuilder
Expand All @@ -11,6 +11,8 @@
from mashumaro.core.meta.types.pack import PackerRegistry
from mashumaro.core.meta.types.unpack import UnpackerRegistry

CALL_EXPR = re.compile(r"^([^ ]+)\(value\)$")


class CodecCodeBuilder(CodeBuilder):
@classmethod
Expand Down Expand Up @@ -48,10 +50,12 @@ def add_decode_method(
)
self.add_line(f"return {unpacked_value}")
self.add_line("setattr(decoder_obj, 'decode', decode)")
if pre_decoder_func is None and is_dataclass(shape_type):
method_name = unpacked_value.partition("(")[0]
self.lines.reset()
self.add_line(f"setattr(decoder_obj, 'decode', {method_name})")
if pre_decoder_func is None:
m = CALL_EXPR.match(unpacked_value)
if m:
method_name = m.group(1)
self.lines.reset()
self.add_line(f"setattr(decoder_obj, 'decode', {method_name})")
self.ensure_object_imported(decoder_obj, "decoder_obj")
self.ensure_object_imported(self.cls, "cls")
self.compile()
Expand Down Expand Up @@ -89,10 +93,12 @@ def add_encode_method(
else:
self.add_line(f"return {packed_value}")
self.add_line("setattr(encoder_obj, 'encode', encode)")
if post_encoder_func is None and is_dataclass(shape_type):
method_name = packed_value.partition("(")[0]
self.lines.reset()
self.add_line(f"setattr(encoder_obj, 'encode', {method_name})")
if post_encoder_func is None:
m = CALL_EXPR.match(packed_value)
if m:
method_name = m.group(1)
self.lines.reset()
self.add_line(f"setattr(encoder_obj, 'encode', {method_name})")
self.ensure_object_imported(encoder_obj, "encoder_obj")
self.ensure_object_imported(self.cls, "cls")
self.ensure_object_imported(self.cls, "self")
Expand Down

0 comments on commit 8597bd9

Please sign in to comment.