Repository navigation
Add opt-in unstructure validation strategy - #845
Open
godara-alt wants to merge 3 commits into
Open
godara-alt wants to merge 3 commits into
godara-alt wants to merge 3 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.