Skip to content

NewType of List, Optional, etc. not supported #1207

@Kazy

Description

@Kazy

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.4
            pydantic compiled: True
                 install path: /home/akear/.pyenv/versions/3.7.6/envs/dev/lib/python3.7/site-packages/pydantic
               python version: 3.7.6 (default, Jan  9 2020, 13:33:30)  [GCC 9.2.0]
                     platform: Linux-5.4.15-zen1-1-zen-x86_64-with-arch
     optional deps. installed: ['typing-extensions', 'devtools']

Hello ! Thank you for pydantic. I've found a bug when using NewType:

import typing as t
import pydantic

Foo = t.NewType("Foo", t.List[int])

class Doc(pydantic.BaseModel):
    foo: Foo

Traceback:

Traceback (most recent call last):
  File "pydantic/validators.py", line 560, in pydantic.validators.find_validators
TypeError: issubclass() arg 1 must be a class

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    class Doc(pydantic.BaseModel):
  File "pydantic/main.py", line 199, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/fields.py", line 277, in pydantic.fields.ModelField.infer
  File "pydantic/fields.py", line 249, in pydantic.fields.ModelField.__init__
  File "pydantic/fields.py", line 335, in pydantic.fields.ModelField.prepare
  File "pydantic/fields.py", line 468, in pydantic.fields.ModelField.populate_validators
  File "pydantic/validators.py", line 569, in find_validators
RuntimeError: error checking inheritance of typing.List[int] (type: List[int])

From what I understand, the current NewType handling done in validators.py:find_validators correctly resolve to typing.List[int] but doesn't know what to do with it since typing.List[int] is normally handled in _type_analysis and find_validators only sees the int type in this case.

Thanks !

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions