python-attrs / python-attrs/cattrs

`cattrs.structure` is failing `detailed_validation=True` behavior with validations

Open
#440 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.