From ef2f050dfa8bfcc33e26818bad523bc33518f02a Mon Sep 17 00:00:00 2001 From: Vansh Jenikumar Barfiwala <227775098+vanshbarfiwala-lgtm@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:31:25 +0530 Subject: [PATCH] fix: match enum-valued Literal discriminators by their unstructured value create_default_dis_func keyed the discriminator mapping by the raw Literal args, but the disambiguator looks them up against the already-unstructured input. For an enum-valued Literal the member is serialized to its value, so the lookup raised KeyError (structure failed while unstructure succeeded). Key the mapping by the unstructured value, which is identity for str/int/bool and the member's value for enums. --- HISTORY.md | 2 ++ src/cattrs/disambiguators.py | 8 ++++++-- tests/test_disambiguators.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 942616e3..9d6b3ad8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -13,6 +13,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python ## NEXT +- Fix a `KeyError` when structuring a union of _attrs_ classes discriminated by an enum-valued `Literal`. The discriminator is now matched by its unstructured value, consistent with string- and integer-valued `Literal` discriminators. + - Fix the msgspec converter passing typed deques directly to the encoder, which cannot serialize them. Deques are now unstructured into lists, including when nested in attrs classes or dataclasses. ([#818](https://github.com/python-attrs/cattrs/issues/818) [#819](https://github.com/python-attrs/cattrs/pull/819)) diff --git a/src/cattrs/disambiguators.py b/src/cattrs/disambiguators.py index 451d5768..3c3db528 100644 --- a/src/cattrs/disambiguators.py +++ b/src/cattrs/disambiguators.py @@ -88,12 +88,16 @@ def create_default_dis_func( best_result = None best_discriminator = None for discriminator in discriminators: - # maps Literal values (strings, ints...) to classes + # maps Literal values (strings, ints, enums...) to classes mapping = defaultdict(list) for cl, candidates in zip(classes, cls_candidates): for key in get_args(candidates[discriminator].type): - mapping[key].append(cl) + # A Literal value may be an enum member, which is serialized + # to its value when unstructured. Key the mapping by the + # unstructured form so lookups against the (already + # unstructured) discriminator in the input data match. + mapping[converter.unstructure(key)].append(cl) if best_result is None or max(len(v) for v in mapping.values()) <= max( len(v) for v in best_result.values() diff --git a/tests/test_disambiguators.py b/tests/test_disambiguators.py index 66f366d8..bb575768 100644 --- a/tests/test_disambiguators.py +++ b/tests/test_disambiguators.py @@ -1,6 +1,7 @@ """Tests for auto-disambiguators.""" from dataclasses import dataclass +from enum import Enum from functools import partial from itertools import permutations from typing import Literal, Union @@ -286,6 +287,33 @@ class K: assert fn({"a": 0}) == Union[J, K] +def test_disambiguate_from_enum_valued_literal(): + """Enum members used as Literal discriminators are matched by value.""" + c = Converter() + + class Species(Enum): + CAT = "cat" + DOG = "dog" + + @define + class Cat: + kind: Literal[Species.CAT] + meow: int = 1 + + @define + class Dog: + kind: Literal[Species.DOG] + woof: int = 2 + + fn = create_default_dis_func(c, Cat, Dog) + assert fn({"kind": "cat"}) is Cat + assert fn({"kind": "dog"}) is Dog + + for v in (Cat(Species.CAT), Dog(Species.DOG)): + u = c.unstructure(v, unstructure_as=Union[Cat, Dog]) + assert c.structure(u, Union[Cat, Dog]) == v + + def test_default_no_literals(): """The default disambiguator can skip literals.""" c = Converter()