Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
8 changes: 6 additions & 2 deletions src/cattrs/disambiguators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 28 additions & 0 deletions tests/test_disambiguators.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down