structure: how to report path to invalid data element
See original GitHub issue- cattrs version: 0.9.0
- Python version: 3.6.ř
- Operating System: Debian 9
Description
I want to cattrs to load complex nested data and in case some validation/conversion fails, I want to provide reasonable context information about what part of data did not work properly.
What I Did
Having attrs based classes: Config with attributes source, fetch and publish, each holding value of specific (attrs based) class Source, Fetch and Publish.
If some data element is wrong (e.g. expecting integer and providing string “5a”), the structure process fails raising ValueError("could not convert string to float: '5a'",)
However, the error does not include any contextual information about where in my nested input the problem was read from.
It would be nice to get some sort of path in the exception, which I could use. marshmallow
and trafaret
are examples of similar solutions providing contextual information.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:8 (4 by maintainers)
This is probably the next big feature I work on 😃
We could copy a few ideas from
jsonschema
, and potentially yield errors iteratively? Or maybe that’s not really in scope, sincecattrs
need to return the new result.But have a look at their ValidationError, there’s a few fields there that we could potentially use.