Skip to content

Add opt-in unstructure validation strategy - #845

Open
godara-alt wants to merge 3 commits into
python-attrs:mainfrom
godara-alt:fix/601-unstructure-validation
Open

godara-alt wants to merge 3 commits into
python-attrs:mainfrom
godara-alt:fix/601-unstructure-validation

Conversation

@godara-alt

Copy link
Copy Markdown

Fixes #601.

The issue occurs when a value assigned to an enum-typed attribute is not an instance of the expected enum. During unstructuring, the converter attempts to access the .value attribute without checking the actual type, resulting in an AttributeError that doesn't clearly explain what went wrong.

Adding validation directly to the existing unstructure hooks would introduce additional overhead for users who don't require runtime type validation. To avoid this, I have taken the opt-in approach suggested in the issue discussion.

Implementation

This change introduces a new validate_unstructure strategy that allows users to enable runtime type validation for selected types on a converter.

The strategy retrieves the existing unstructure hooks and wraps them with an instance check. If the provided value matches the expected type, the original hook handles it as usual. Otherwise, a TypeError is raised with the expected and received types.

Existing custom hooks are preserved, and subclasses of the expected type are accepted. The implementation also handles multiple selected types, inheritance, typed enums and invalid typing forms consistently across supported Python versions.

The default unstructuring behavior remains unchanged, so users who do not enable this strategy will not incur additional validation overhead.

Usage

from enum import Enum

from attrs import define
from cattrs import Converter
from cattrs.strategies import validate_unstructure

class Flavor(Enum):
SIMPLE = "simple"

@define
class Site:
flavor: Flavor

converter = Converter()
validate_unstructure(converter, Flavor)

converter.unstructure(Site(Flavor.SIMPLE))

{'flavor': 'simple'}

converter.unstructure(Site("simple"))

TypeError: Expected Flavor, got str

Testing and verification

Added 25 regression test cases covering valid and invalid enum values, attrs classes, dataclasses, nested collections, custom unstructure hooks, inheritance, converter copying, converter isolation and invalid type configurations.

The changes have been tested across Python 3.10, 3.11, 3.12, 3.13, 3.14, 3.15 and PyPy 3.10.

All 12 CI jobs passed on the fork's verification branch, including the complete Python test matrix, lint checks, package build and verification, combined code coverage, security checks and all 64 CodSpeed benchmarks.

The Python 3.13 test suite completed with 1,098 passed tests, and combined code coverage reached 100%.

The verification branch includes a separate CodSpeed runner compatibility change that is not included in this PR. The production code, documentation and regression tests are identical between both branches.

CI results

Main CI and coverage:
https://github.com/godara-alt/cattrs/actions/runs/47079176846

CodSpeed benchmarks:
https://github.com/godara-alt/cattrs/actions/runs/47079177135

Security checks:
https://github.com/godara-alt/cattrs/actions/runs/47079176832

The strategy documentation, README and changelog have also been updated to explain the new functionality and its intended usage.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum misuse causes unhelpful error

1 participant