Pydantic: AssertionError on class creation if typing.Type is used

Created on 19 Jul 2019  路  7Comments  路  Source: samuelcolvin/pydantic


Bug | Feature request

When I create model and using typing.Type annotation, AssertionError gets raised immediately:

from typing import Type, Any
from pydantic import BaseModel

class Model(BaseModel):
    obj: Type[Any]  # Could be any type there, e. g. Type[str]

```py
Traceback (most recent call last):
class Model(BaseModel):
File "pydantic/main.py", line 209, in pydantic.main.MetaModel.__new__
File "pydantic/fields.py", line 135, in pydantic.fields.Field.infer
File "pydantic/fields.py", line 112, in pydantic.fields.Field.__init__
File "pydantic/fields.py", line 177, in pydantic.fields.Field.prepare
File "pydantic/fields.py", line 238, in pydantic.fields.Field._populate_sub_fields
AssertionError

AssertionError is raised on this line, `origin` is `type` here:
https://github.com/samuelcolvin/pydantic/blob/18d4b2bb2ac9790bd58780dba67947a088725731/pydantic/fields.py#L252

### Upd:
Also just discovered that if we replace `Type[Any]` with `type`, will get this:
```py
Traceback (most recent call last):
    class Model(BaseModel):
  File "pydantic/main.py", line 209, in pydantic.main.MetaModel.__new__
  File "pydantic/fields.py", line 135, in pydantic.fields.Field.infer
  File "pydantic/fields.py", line 112, in pydantic.fields.Field.__init__
  File "pydantic/fields.py", line 178, in pydantic.fields.Field.prepare
  File "pydantic/fields.py", line 269, in pydantic.fields.Field._populate_validators
  File "pydantic/validators.py", line 475, in find_validators
RuntimeError: no validator found for <class 'type'>

This can be bypassed by setting Confif.arbitrary_types_allowed to True

  • OS: Ubuntu 18.10
  • Python version: 3.7.3 (default, Apr 9 2019, 04:56:51) [GCC 8.3.0], (same on 3.6.8)
  • Pydantic version: 0.30 (same on 0.30.1)
feature request help wanted

Most helpful comment

[joke mode on, don't take it too seriously]
@MrMrRobat is this one of those recursion jokes? -> Your comment on a fix (https://github.com/samuelcolvin/pydantic/issues/678#issuecomment-538802546) is linking to the same issue, this one to be exact ;)
[joke mode off]

On a serious note - is the fix in some other place?

All 7 comments

I guess Currently this "type" is not supported, PR welcome to implement it.

Maybe also replace bare AssertionError with TypeError(f'Type {origin} is currently not supported')?

yes agreed.

Seems to be fixed by #678

[joke mode on, don't take it too seriously]
@MrMrRobat is this one of those recursion jokes? -> Your comment on a fix (https://github.com/samuelcolvin/pydantic/issues/678#issuecomment-538802546) is linking to the same issue, this one to be exact ;)
[joke mode off]

On a serious note - is the fix in some other place?

Another serious question, what is typing.Type used for? I've not seen that, and instead usually see the bare type, e.g. value: str or value: Any, etc.

well, the same stuff it's used for in the python docs - defining class types, rather than instances.

Was this page helpful?
0 / 5 - 0 ratings