Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "Field()" on dataclasses #470

Closed
mariusvniekerk opened this issue Apr 9, 2019 · 5 comments · Fixed by #2384
Closed

Support "Field()" on dataclasses #470

mariusvniekerk opened this issue Apr 9, 2019 · 5 comments · Fixed by #2384
Labels
feature request help wanted Pull Request welcome

Comments

@mariusvniekerk
Copy link

Bug

When using pydantic.Schema in combination with optional

import pydantic

from pydantic.dataclasses import dataclass

@dataclass
class User:
    id: int
    name: pydantic.NoneStr = pydantic.Schema(None)

User(4)
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-24-c6a72a265df5> in <module>
      8     name: pydantic.NoneStr = pydantic.Schema(None)
      9 
---> 10 User(4)

<string> in __init__(self, id, name)

~/miniconda3/lib/python3.6/site-packages/pydantic/dataclasses.py in _pydantic_post_init(self)
     25 
     26 def _pydantic_post_init(self: 'DataclassType') -> None:
---> 27     d = validate_model(self.__pydantic_model__, self.__dict__)
     28     object.__setattr__(self, '__dict__', d)
     29     object.__setattr__(self, '__initialised__', True)

~/miniconda3/lib/python3.6/site-packages/pydantic/main.py in validate_model(model, input_data, raise_exc)
    625 
    626     if errors:
--> 627         raise ValidationError(errors)
    628     return values

ValidationError: 2 validation errors
name
  str type expected (type=type_error.str)
name
  value is not none (type=type_error.none.allowed)

When calling it with all the arguments filled it works as expected

In  [2]: User(4, None)
Out [2]: User(id=4, name=None)

When not using the pydantic.Schema this also behaves as expected
@samuelcolvin
Copy link
Member

I think pydantic.dataclasses isn't setup to work with Schema, shouldn't be that hard to fix. PR welcome.

@samuelcolvin samuelcolvin added bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome labels Apr 15, 2019
@Benoss
Copy link

Benoss commented Apr 26, 2019

Probably related to this, you can't export a schema with dataclasses.
I have to use BaseModel wich is fine but I am loosing the nice autocomplete and liinting from PyCharm by not using @dataclass.I tried to use both BaseModel and @DataClass but you get a recursion error.

from pydantic.schema import schema
from pydantic.dataclasses import dataclass

@dataclass
class Foo:
    a: str = None

@dataclass
class Bar:
    b: Foo
    name: str

top_level_schema = schema([Foo, Bar])
print(top_level_schema)

AttributeError: type object 'Foo' has no attribute '__fields__'

@Midnighter
Copy link
Contributor

You should use the underlying pydantic model:

top_level_schema = schema([Foo.__pydantic_model__, Bar.__pydantic_model__])

it's not yet in the documentation but I found it here in a few issues.

@therefromhere
Copy link
Contributor

A related (maybe the same?) issue on using Schema with dataclasses if anyone's working on it: default value isn't used, instead you get the Schema instance.

import pydantic
import pydantic.dataclasses

@pydantic.dataclasses.dataclass 
class UserDc: 
    name: pydantic.Any = pydantic.Schema(None) 

class User(pydantic.BaseModel): 
    name: pydantic.Any = pydantic.Schema(None) 
In [2]: repr(UserDc().name)                                                                                    
Out[2]: 'Schema(extra: {})'

In [3]: repr(User().name)                                                                                      
Out[3]: 'None'

@samuelcolvin
Copy link
Member

Similar issue, again shouldn't be that hard to fix if someone wants to work on it.

@samuelcolvin samuelcolvin added feature request and removed bug V1 Bug related to Pydantic V1.X labels Oct 17, 2019
@samuelcolvin samuelcolvin changed the title dataclass issue around Optional types Support "Field()" on dataclasses Oct 17, 2019
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants