Proposal: Support for dataclasses (PEP-557)
See original GitHub issueUse-case abstract
Validate instances of dataclasses with Cerberus.
Proposal
@rhettinger concluded his talk at PyCon about dataclasses with the outlook that third-party-libraries would take the task to validate these. inevitably i thought about enabling Cerberus to do so.
hence i propose to add a module cerberus.dataclasses
that would provide the following features:
- a
Validator
class that extendscerberus.validator.Validator
- takes dataclass instances as input beside mappings
- derives a schema from:
- a field’s type annotation (*)
a field’simplemented by a dataclassdefault
ordefault_factory
setting- rules that are provided as a field’s
metadata['cerberus']
value
- validates against a mapping representation of a dataclass against that
- fields with dataclass instances as value are handled accordingly (nesting)
- a
validate
function- follows the paradigm of the stdlib’s
dataclasses
module - a customized validator can be provided as keyword argument
- it should be capable to return the used validator for error inspection (, when a keyword argument is given or by default?)
- follows the paradigm of the stdlib’s
- as i assume there’ll be no way to not support normalization, the functions
normalized
andvalidated
as well
*: as type information are provided as annotations in a dataclass’ metadata, Cerberus’ lack of capability to validate against such constraints is a showstopper for this feature.
while this feature could be postponed to a post-2.0
-release, the outlook on this feature is rather increasing my drive to get started with the 2.x
branch at all.
reminder: there’s a backport for Python 3.6.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:5 (4 by maintainers)
Top Results From Across the Web
PEP 557 – Data Classes - Python Enhancement Proposals
Using these fields, the decorator adds generated method definitions to the class to support instance initialization, a repr, comparison methods, and optionally ...
Read more >Support inheritance for Data Classes (PEP 557) : PY-28506
Support inheritance for Data Classes (PEP 557) ... PY-34566 Dataclass: No proposals for parent class shown when instantiating child class.
Read more >Dataclasses and attrs: when and why - REVSYS
Python 3.7 introduced dataclasses (PEP557). Dataclasses can be a convenient way to generate classes whose primary goal is to contain values.
Read more >Re: [Python-Dev] Replacement proposal for PEP 557 Data Classes
My solution is (IMHO) more concise and brings more features. > So I wanted to make it a PEP until I saw PEP...
Read more >PEP 557 (Data Classes) has been accepted! : r/Python - Reddit
I hate that this has a completely different syntax than namedtuple. They offer the same exact functionality (data objects without a tonne of ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Let’s do this.
thank you very much. this is a good impulse to figure out design details. i want to point that i only have brief practical expierence with the
dataclasses
module. some thoughts about your gist:i see that the docs hardly the introspective properties of dataclasses. the
__dataclass_fields__
property should be used to as sole source to derive from.i’d really prefer to use a field’s
type
property as constraint for thetype
rule in the derived schema. that way the code is short, abstract and it can handle any input.what about
typing.Union
andtyping.Optional
?at least these rules should be dropped if present in a provided rules set and a critical warning be emitted:
required
,default
,default_setter
. oh, that should be implemented with a proper schema that validates that a validator is about to use. i’m too tited for details, that stuff lives incerberus.schema
. it’s kinda messy to get into it; that refactor for Python 3 is more convenient to read to get a general idea. maybe the dataclass validator could specify a schema for these rules that unconditionally raises an exception in acheck_with
handler as rules can’t be removed with a subclass so far (with a stacked metaclass?!)have a good night.