From 69d7ed60dab7c75d53692e833e24598b9f4331d2 Mon Sep 17 00:00:00 2001 From: Alexander Tikhonov Date: Thu, 26 Sep 2024 17:05:19 +0300 Subject: [PATCH] Avoid potential duplications in union unpacker --- mashumaro/core/meta/types/unpack.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mashumaro/core/meta/types/unpack.py b/mashumaro/core/meta/types/unpack.py index 9c040d5..43b8d8d 100644 --- a/mashumaro/core/meta/types/unpack.py +++ b/mashumaro/core/meta/types/unpack.py @@ -170,15 +170,19 @@ def get_method_prefix(self) -> str: def _add_body(self, spec: ValueSpec, lines: CodeLines) -> None: ambiguous_unpacker_types = [] + unpackers = set() for type_arg in self.union_args: unpacker = UnpackerRegistry.get( spec.copy(type=type_arg, expression="value") ) if type_arg in (bool, str) and unpacker == "value": ambiguous_unpacker_types.append(type_arg) + if unpacker in unpackers: + continue with lines.indent("try:"): lines.append(f"return {unpacker}") lines.append("except Exception: pass") + unpackers.add(unpacker) # if len(ambiguous_unpacker_types) >= 2: # warnings.warn( # f"{type_name(spec.builder.cls)}.{spec.field_ctx.name} "