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

Error in find_validators (issubclass() arg 1 must be a class) #1722

Closed
jbeezley opened this issue Jul 14, 2020 · 1 comment · Fixed by #1712
Closed

Error in find_validators (issubclass() arg 1 must be a class) #1722

jbeezley opened this issue Jul 14, 2020 · 1 comment · Fixed by #1712
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@jbeezley
Copy link

Bug

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

             pydantic version: 1.6
            pydantic compiled: True
                 install path: <prefix>/env/lib/python3.8/site-packages/pydantic
               python version: 3.8.2 (default, Apr 27 2020, 15:53:34)  [GCC 9.3.0]
                     platform: Linux-5.4.0-39-generic-x86_64-with-glibc2.29
     optional deps. installed: ['typing-extensions']

from typing import List

from pydantic import BaseModel, Field, validator


class Schema(BaseModel):
    foo: List[str] = Field(default_factory=list)

    @validator("foo", pre=True, each_item=True)
    def mutate_foo(cls, v):
        return f"{v}-1"


class Derived(Schema):
    pass


print(Derived(foo=['bar']).foo)

This results in

Traceback (most recent call last):
  File "pydantic/validators.py", line 579, 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 "ptest.py", line 14, in <module>
    class Derived(Schema):
  File "pydantic/main.py", line 231, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/fields.py", line 523, in pydantic.fields.ModelField.populate_validators
  File "pydantic/validators.py", line 588, in find_validators
RuntimeError: error checking inheritance of typing.List[str] (type: List[str])

This code sample works in pydantic 1.5.1. The bug seems to rely on the combination of default_factory, the @validator method, and the subclass. Removing any one of these causes the code to run correctly.

Note: there are a few other issues with the similar error message, but they appear to be distinct from this because they apply to versions <= 1.5.1 and have slightly different stack traces.

@jbeezley jbeezley added the bug V1 Bug related to Pydantic V1.X label Jul 14, 2020
jbeezley added a commit to microbiomedata/nmdc-server that referenced this issue Jul 14, 2020
@PrettyWood
Copy link
Member

PrettyWood commented Jul 14, 2020

Hello @jbeezley
There is already a fix on default_factory #1712, which should fix your issue as well (instead of the error you get the expected ['bar-1']). I updated the description of the PR to add your issue.
@samuelcolvin will probably ship a patched version 1.6.1 soon if the fix is approved.
I'm really sorry for the inconvenience.

samuelcolvin added a commit to PrettyWood/pydantic that referenced this issue Jul 15, 2020
samuelcolvin added a commit that referenced this issue Jul 15, 2020
…#1712)

* fix: validate nested models with `default_factory`

PR #1504 introduced a regression by bypassing `populate_validators()`,
which would skip the validation of children in nested models
with `default_factory`

closes #1710

* test: add example of nested models parsing with `default_factory`

closes #1717

* add testcase from #1722

* bodge for benchmarks

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants