Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Data validation on attribute setting? #117

Closed
cdeil opened this issue Nov 19, 2019 · 2 comments
Closed

Data validation on attribute setting? #117

cdeil opened this issue Nov 19, 2019 · 2 comments
Labels
wontfix This will not be worked on

Comments

@cdeil
Copy link

cdeil commented Nov 19, 2019

Is there a way to get data validation on attribute setting?

Can it be achieved with your package e.g. by subclassing and overwriting __setattribute__?

Or if this not in scope, is there some other package you can recommend?

@Bultako and I are looking for a solution to have hierarchical config objects that offer dot access and do type validation (or more, a la JSON schema) as users interact with the object. Basically the structure of the config should be frozen, types should be fixed, only values should be allowed to change.

from dataclasses import dataclass
from dataclasses_jsonschema import JsonSchemaMixin

@dataclass
class Point(JsonSchemaMixin):
    "A 2D point"
    x: float
    y: float

p  = Point(3, 4)

I'd like this to raise an error:

p.x = "not a number, please no"
@s-knibbs s-knibbs added the wontfix This will not be worked on label Nov 19, 2019
@s-knibbs
Copy link
Owner

This wouldn't be easy to implement because all validation is handled by the jsonschema library which only validates the entire object.

You can use mypy to catch any type mismatches at compile time though. Mypy will generate an error for the example above.

@cdeil
Copy link
Author

cdeil commented Nov 19, 2019

Our use case is for configuration, where users sometimes write it in YAML, and sometimes they then interactively change config items at runtime, e.g.:

config.analysis.debug = True

and we want this to fail immediately

config.analysis.debug = "yes, please"

For our use case performance isn't a concern at all, always re-validating the whole object would be fine. But I guess it's not possible or easy if the schema is for config and then it should run when config.analysis.debug.__setattr__ runs.

Thanks for the quick reply!

I found this just now : pydantic/pydantic#94
will keep looking for options.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants