Pydantic: Recursive models throw an exception with Python 3.9.0

Created on 9 Oct 2020  路  3Comments  路  Source: samuelcolvin/pydantic

Checks

  • [x] I added a descriptive title to this issue
  • [x] I have searched (google, github) for similar issues and couldn't find anything
  • [x] I have read and followed the docs and still think this is a bug

Bug

If you have a recursive model and call update_forward_refs() on it, it throws an exception like:

File ".../test_pydantic.py", line 8, in <module>
    Schema.update_forward_refs(recursive_guard=set())
  File ".../.venv/lib/python3.9/site-packages/pydantic/main.py", line 677, in update_forward_refs
    update_field_forward_refs(f, globalns=globalns, localns=localns)
  File ".../.venv/lib/python3.9/site-packages/pydantic/typing.py", line 233, in update_field_forward_refs
    field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
  File ".../.venv/lib/python3.9/site-packages/pydantic/typing.py", line 50, in evaluate_forwardref
    return type_._evaluate(globalns, localns)
TypeError: _evaluate() missing 1 required positional argument: 'recursive_guard'

Seems to be coming from this line which calls an internal method that now requires a new argument. It seems like (from some very cursory testing) you could just pass recursive_guard=set() on this line if the version is 3.9.x? Not sure if that's the _right_ solution but it did stop my code from crashing.

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

pydantic version: 1.6.1
            pydantic compiled: False
                 install path: .../.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.0 (default, Oct  6 2020, 04:17:54)  [Clang 12.0.0 (clang-1200.0.32.2)]
                     platform: macOS-10.15.7-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

To Reproduce

from pydantic import BaseModel


class Schema(BaseModel):
    another: "Schema"


Schema.update_forward_refs()
bug

Most helpful comment

Looks like this is covered in #1844 so doesn't need to be addressed independently. I'm going to leave the ticket around so others will find it when encountering this issue.

All 3 comments

Looks like this is covered in #1844 so doesn't need to be addressed independently. I'm going to leave the ticket around so others will find it when encountering this issue.

Looks like this is covered in #1844 so doesn't need to be addressed independently. I'm going to leave the ticket around so others will find it when encountering this issue.

Thanks for leaving this up! I just ran into this issue.

Looks like this is covered in #1844 so doesn't need to be addressed independently. I'm going to leave the ticket around so others will find it when encountering this issue.

Thanks for leaving this up! I just ran into this issue.

Me too. Looking forward to the fix!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashpreetbedi picture ashpreetbedi  路  3Comments

gangefors picture gangefors  路  3Comments

dconathan picture dconathan  路  3Comments

krzysieqq picture krzysieqq  路  3Comments

timonbimon picture timonbimon  路  3Comments