I don't know if this is supposed to work. Would be nice. I can try to fix it if you propose the way.
@dataclasses.dataclass
class C(pydantic.BaseModel):
i:int
c = C(1)
....
RecursionError: maximum recursion depth exceeded
It's not, I haven't really looked into dataclass yet.
I'm not exactly clear on what they do or why they'd be useful with pydantic. Could you explain?
It seems that pydantic offers similar functionality, but dataclasses are going to be a standard way how to write value classes in the future. It would be nice, if we can use pydantic for runtime validation of dataclasses.
simple solution is to don't use dataclass __init__ method
@dataclasses.dataclass(init=False)
class C(pydantic.BaseModel):
i:int
but the pydantic's __init__ method does not accept positional arguments, so the code using dataclasses can't start using pydantic easily.
It would be freaking awesome we could use pydantic's validation in Python's dataclasses.
@pawelswiecki sounds good to me, PR welcome.
Using init=False (@dataclasses.dataclass(init=False)) indeed fixes maximum recursion issue.
Quick poking around with instances of class defined this way (that is with both @dataclass decorator and inheriting from pydantic.BaseModel) results in an optimistic conclusion: it does work and the object behaves as both dataclass and pydantic's validator. I will possibly try to use this combination in some cases. Will report here if there's anything to report.
Proposal:
pydantic adds a decorator to validate dataclasses input data and equivalently it's own version of dataclass which performs the same as normal dataclasses except for validating input data.
Something like:
from dataclasses import dataclasee
import pydantic
@pydantic.dataclasses.validate
@dataclass
class InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0
# which would be exactly the same as:
@pydantic.dataclasses.dataclass
class InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0
@Gaunt @pawelswiecki, if this was achievable, would be work for you?
This would require a significant rewrite of pydantic, but I think it would be worth it to support a big new language feature.
As a small FYI, there's a backport of dataclasses to Python 3.6: https://github.com/ericvsmith/dataclasses
why is a significant rewrite needed? Seems to me that the functions for validation are there already.
you can create a pydantic model from dataclass like this
def pydantic_model(cls):
fields = {name: (field.type, field.default)
for name, field
in cls.__dataclass_fields__.items()}
model = create_model('', **fields)
return model
then use validate_model function from pydantic
can I send a PR?
Yes that's a way to do it. It would require support for positional arguments with don't currently work.
the delta b/w dataclasses and pydantic are: serialization/deserialization, auto schema, and validation. i love pedantic but it seems like it's only a matter of time before dataclasses gets such features. if not, then ya the decorator idea would keep pedantic relevant.
I disagree on the point about data classes: I don’t think it will get rich serialization and validation in the core. The standard library is very conservative.
—Paul
On Jul 15, 2018, at 7:23 PM, Majid alDosari notifications@github.com wrote:
the delta b/w dataclasses and pydantic are: serialization/deserialization, auto schema, and validation. i love pedantic but it seems like it's only a matter of time before dataclasses gets such features. if not, then ya the decorator idea would keep pedantic relevant.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/samuelcolvin/pydantic/issues/178#issuecomment-405126033, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbx5m68gTyvsPKMvxrbrY36HktEim7Wks5uG87VgaJpZM4UBBf7.
hi, I have returned from my vacation yesterday. Sorry for the delay. I wanted to understand the code better. I will finish it tomorrow.
One nice side effect of the dataclass work in the PR. In my IDE (PyCharm) I now get autocomplete and warnings when constructing a model instance.
Dataclasses now merged, I'll release a new version of pydantic in a couple of days.
I haven't looked at the code of pydantic, but I guess you don't inherit from dataclasses.dataclass since I'm missing useful features I had expected when I checked https://docs.python.org/3/library/dataclasses.html - fields and asdict. Do you need a hand with that?
we do use standard dataclasses (let's not use inherit here, since we're not talking about class inheritance).
The reason you're not seeing asdict is because the 3.6 backport of dataclasses doesn't implement it.
# python 3.6
from dataclasses import dataclass
@dataclass
class InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0
def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand
item = InventoryItem('hammers', 10.49, 12)
item.asdict()
#> AttributeError: 'InventoryItem' object has no attribute 'asdict'
I see. Confusing :p
Keep up the good work! Finally a good use for the typing module ;-)
Constantly coming up with new ways to use pydantic in my projects, it's really fun!
Finally a good use for the typing module
:+1:
Most helpful comment
Proposal:
pydantic adds a decorator to validate dataclasses input data and equivalently it's own version of
dataclasswhich performs the same as normal dataclasses except for validating input data.Something like:
@Gaunt @pawelswiecki, if this was achievable, would be work for you?
This would require a significant rewrite of pydantic, but I think it would be worth it to support a big new language feature.