Pydantic: allow arbitary types

Created on 23 May 2018  路  11Comments  路  Source: samuelcolvin/pydantic

Remove the exception pydantic.exceptions.ConfigError: no validator found for <class 'Foobar'> and just require that any value provided is an instance of Foobar.

feature request help wanted

Most helpful comment

@mr-bjerre A ctrl+f for arbitrary_types_allowed on the pydantic docs should get you to the right place.

Here's an demonstration that I believe should work (not tested):

from pydantic import BaseModel

class MyClass: 
    pass

class Model(BaseModel):
    v: MyClass

    class Config:
        arbitrary_types_allowed = True

All 11 comments

I'm not sure whether I understand the issue correctly. Does it cover the following case which raises exception mentioned by You?

from datetime import datetime
from typing import List
from pydantic import BaseModel

class MyClass:
    pass

class User(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: datetime = None
    friends: List[int] = []
    my: MyClass

external_data = {'id': '123', 'signup_ts': '2017-06-01 12:22', 'friends': [1, '2', b'3'], 'my': MyClass()}
user = User(**external_data)

Exactly the user case.

I guess we need a config variable to enable/disable this.

implemented in #209

Is this supposed to be fixed? I still get an error

Yes.

Are you using arbitrary_types_allowed = True?

Aha no. Where to set that? As an attribute to the class?

please read the documentation.

Do you mind pointing to where in the documentation? I didn鈥檛 find this thread by not looking. I was actively looking for it in the docs.

Well knowing the keyword arbitrary_types_allowed made the search easier. Thank you

@mr-bjerre A ctrl+f for arbitrary_types_allowed on the pydantic docs should get you to the right place.

Here's an demonstration that I believe should work (not tested):

from pydantic import BaseModel

class MyClass: 
    pass

class Model(BaseModel):
    v: MyClass

    class Config:
        arbitrary_types_allowed = True

I was affected by this error and this thread (and especially the example by @dmontagu ) helped me solving it.

However:

@mr-bjerre A ctrl+f for arbitrary_types_allowed on the pydantic docs should get you to the right place.

Either I am surprisingly blind on this or something has changed: If I open the linked page in the docs https://pydantic-docs.helpmanual.io/ I cannot find anything for arbitrary_types_allowed or meaningful substrings. I also have not found a global search function. The only way I could find this in the docs was to manually open subpages which sound somehow relevant and using ctrl-f there. Result: https://pydantic-docs.helpmanual.io/usage/dataclasses/#use-custom-types.

Is there a better strategy to find the relevant section in the docs?

Was this page helpful?
0 / 5 - 0 ratings