python-attrs / python-attrs/cattrs
`cattrs.structure` is failing `detailed_validation=True` behavior with validations
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
Description
- cattrs version: 23.1.2
- Python version: 3.10
- Operating System: windows 10
- IDE: Pycharm 2023.1 Professional Edition
Description
cattrs.structure is seemingly failing to properly aggregate validation failures, even when explicitly setting detailed_validation=True on new converter.
What I Did
This will result in ['invalid value @ $'] which is unexpected.
import cattrs
from attrs import define
from attrs import field, validators
@define
class TestModel:
test_data_1: float = field(validator=[validators.instance_of(float), validators.lt(100)])
test_data_2: str = field(validator=validators.max_len(3))
@test_data_1.validator
def _check_scale(self, attribute, value):
if int(log10(value)) + 1 > 1:
raise ValueError("value significant figures is greater than 1")
data = {"test_data_1": 110, "test_data_2": "abcdefj"}
try:
print(cattrs.structure(data, TestModel))
except cattrs.BaseValidationError as e:
print(cattrs.transform_error(e))
If I print out the exceptions from e: (ValueError("'test_data_1' must be < 100: 110.0"),)
Unless I'm missing something very critical, this seems like a bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided reproduction against cattrs 23.1.2, focusing on cattrs.structure with detailed validation and the resulting cattrs.BaseValidationError. Inspect how validation failures are aggregated and rendered by cattrs.transform_error; done means the reported result includes the validation failures for both test_data_1 and test_data_2 rather than only one error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100