8000 Specify recursive_guard as kwarg in FutureRef._evaluate by vfazio · Pull Request #9612 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Specify recursive_guard as kwarg in FutureRef._evaluate #9612

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

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

vfazio
Copy link
@vfazio vfazio commented Jun 10, 2024

In CPython 3.12.4/3.13, the function signature of FutureRef._evaluate changed such that recursive_guard is no longer a positional argument; it is now keyword only 0 1.

To accommodate this, specify recursive_guard as a kwarg. This syntax is backwards compatible with earlier versions of the function signature.

Change Summary

Related issue number

Fix #9607 #9609

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

skip change file check

In CPython 3.12.4/3.13, the function signature of `FutureRef._evaluate`
changed such that `recursive_guard` is no longer a positional argument;
it is now keyword only [0][1].

To accommodate this, specify `recursive_guard` as a kwarg. This syntax
is backwards compatible with earlier versions of the function signature.

[0]: python/cpython#118104
[1]: python/cpython#118009

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
@vfazio
Copy link
Author
vfazio commented Jun 10, 2024

Note that there are future problems for pydantic in CPython 3.13, but I will open a new issue for that.

#9613

@sydney-runkle
Copy link
Contributor

Great, thanks!

Copy link
Contributor
@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome job, thank you!

@ostefano
Copy link

@sydney-runkle is a new release already scheduled to include this?

@sydney-runkle
Copy link
Contributor

@ostefano,

We're planning on a V1 and V2 release at the end of June, but maybe this is pressing enough that we should do a patch. I'll chat with the team this morning and see what they think.

@ostefano
Copy link

Thank you @sydney-runkle .

Yes, unless there is a workaround / sanctioned "monkey patch", I believe this to be somehow pressing.

@vfazio
Copy link
Author
vfazio commented Jun 10, 2024

To be transparent, I didn't have time to actually test this under 3.12.4. I don't see the 1.10.x CI matrix running on Python 3.12 at all

If we wanted warm and fuzzies, maybe there should be a quick CI update to widen the CI matrix and also add the 3.12 classifier in setup.py?

What's the EOL for pydantic v1? Should there be a cap on the required python to be <3.13 ?

@vfazio
Copy link
Author
vfazio commented Jun 10, 2024

While it was irresponsible of me, the fix does work:

Failure to import fastapi on 3.12.4 w/ pydantic 1.10.15

(venv) root@1f9ac5aadd81:/# pip install --force pydantic==1.10.15
Collecting pydantic==1.10.15
  Using cached pydantic-1.10.15-py3-none-any.whl.metadata (150 kB)
Collecting typing-extensions>=4.2.0 (from pydantic==1.10.15)
  Using cached typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)
Using cached pydantic-1.10.15-py3-none-any.whl (159 kB)
Using cached typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Installing collected packages: typing-extensions, pydantic
  Attempting uninstall: typing-extensions
    Found existing installation: typing_extensions 4.12.2
    Uninstalling typing_extensions-4.12.2:
      Successfully uninstalled typing_extensions-4.12.2
  Attempting uninstall: pydantic
    Found existing installation: pydantic 1.10.15
    Uninstalling pydantic-1.10.15:
      Successfully uninstalled pydantic-1.10.15
Successfully installed pydantic-1.10.15 typing-extensions-4.12.2
(venv) root@1f9ac5aadd81:/# python3 -c "import fastapi"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/venv/lib/python3.12/site-packages/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/venv/lib/python3.12/site-packages/fastapi/applications.py", line 16, in <module>
    from fastapi import routing
  File "/venv/lib/python3.12/site-packages/fastapi/routing.py", line 22, in <module>
    from fastapi import params
  File "/venv/lib/python3.12/site-packages/fastapi/params.py", line 5, in <module>
    from fastapi.openapi.models import Example
  File "/venv/lib/python3.12/site-packages/fastapi/openapi/models.py", line 124, in <module>
    class Schema(BaseModelWithConfig):
  File "/venv/lib/python3.12/site-packages/pydantic/main.py", line 286, in __new__
    cls.__try_update_forward_refs__()
  File "/venv/lib/python3.12/site-packages/pydantic/main.py", line 807, in __try_update_forward_refs__
    update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))
  File "/venv/lib/python3.12/site-packages/pydantic/typing.py", line 554, in update_model_forward_refs
    update_field_forward_refs(f, globalns=globalns, localns=localns)
  File "/venv/lib/python3.12/site-packages/pydantic/typing.py", line 520, in update_field_forward_refs
    field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pydantic/typing.py", line 66, in evaluate_forwardref
    return cast(Any, type_)._evaluate(globalns, localns, set())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

Fixed on new hash

(venv) root@1f9ac5aadd81:/# pip install --force git+https://github.com/pydantic/pydantic.git@464ed49b1f813103a49116476bec75a94492b338
Collecting git+https://github.com/pydantic/pydantic.git@464ed49b1f813103a49116476bec75a94492b338
  Cloning https://github.com/pydantic/pydantic.git (to revision 464ed49b1f813103a49116476bec75a94492b338) to /tmp/pip-req-build-ef3jn9pi
  Running command git clone --filter=blob:none --quiet https://github.com/pydantic/pydantic.git /tmp/pip-req-build-ef3jn9pi
  Running command git rev-parse -q --verify 'sha^464ed49b1f813103a49116476bec75a94492b338'
  Running command git fetch -q https://github.com/pydantic/pydantic.git 464ed49b1f813103a49116476bec75a94492b338
  Running command git checkout -q 464ed49b1f813103a49116476bec75a94492b338
  Resolved https://github.com/pydantic/pydantic.git to commit 464ed49b1f813103a49116476bec75a94492b338
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Collecting typing-extensions>=4.2.0 (from pydantic==1.10.15)
  Using cached typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)
Using cached typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Building wheels for collected packages: pydantic
  Building wheel for pydantic (pyproject.toml) ... done
  Created wheel for pydantic: filename=pydantic-1.10.15-py3-none-any.whl size=160302 sha256=083532569f8a0eaf5691e0dcfbf388749e0613712a709501b6cc8b5bca97b71b
  Stored in directory: /root/.cache/pip/wheels/d4/39/fc/e66b746c38076ff46a0c011166d066c4e5ebb6df6f7203e770
Successfully built pydantic
Installing collected packages: typing-extensions, pydantic
  Attempting uninstall: typing-extensions
    Found existing installation: typing_extensions 4.12.2
    Uninstalling typing_extensions-4.12.2:
      Successfully uninstalled typing_extensions-4.12.2
  Attempting uninstall: pydantic
    Found existing installation: pydantic 1.10.15
    Uninstalling pydantic-1.10.15:
      Successfully uninstalled pydantic-1.10.15
Successfully installed pydantic-1.10.15 typing-extensions-4.12.2
(venv) root@1f9ac5aadd81:/# python3 -c "import fastapi"

@sydney-runkle
Copy link
Contributor

@vfazio,

Feel free to open a PR to widen the CI matrix, that sounds great. EOL is June 30th, 2024 (so coming up soon). I think a cap on the required version would be good!

@ostefano,

I suppose you could use the change made in this PR as a monkey patch for now while we work on the next release.

@vfazio
Copy link
Author
vfazio commented Jun 10, 2024

@sydney-runkle there are places in the documentation that call out 3.11 specifically (see https://docs.pydantic.dev/latest/contributing/#prerequisites) and that's for current documentation. Before I make this PR, is 3.12 support something pydantic actually wants to advertise support for on 1.10.x?

@sydney-runkle
Copy link
Contributor

EOL is June 30th, 2024 (so coming up soon).

Slight modification here - we'll likely still do fixes like this one to support the still-large number of V1 users 🚀.

@vfazio,

If all tests pass when we add the 3.12 matrices, then I think we can advertise it :).

@vfazio
Copy link
Author
vfazio commented Jun 10, 2024

I'm going to open an issue to continue the discussion. I don't think it's quite as straightforward as I was hoping.

@ostefano
Copy link

For anybody facing this issue when using langchain, the right module to monkey patch is pydantic.v1.typing.

@vfazio
Copy link
Author
vfazio commented Jun 10, 2024

I'm going to open an issue to continue the discussion. I don't think it's quite as straightforward as I was hoping.

#9622

RyoJerryYu added a commit to RyoJerryYu/memos-webhook-py that referenced this pull request Jun 15, 2024
niderhoff referenced this pull request in Homebrew/homebrew-core Jun 17, 2024
sergey-bondarenko-qs pushed a commit to sergey-bondarenko-qs/winter that referenced this pull request Jul 22, 2024
mofr pushed a commit to WinterFramework/winter that referenced this pull request Jul 24, 2024
* Add support of python 3.12
* Replaced openapi-schema-pydantic with openapi-pydantic
* 3.12.4 is not suitable - pydantic/pydantic#9612

---------

Co-authored-by: Sergey Bondarenko <>
scy added a commit to AKVorrat/dearmep that referenced this pull request Nov 13, 2024
This should fix errors in combination with Python 3.12, e.g. in build
<https://github.com/AKVorrat/dearmep/actions/runs/11783011436/job/32819299151>,
which had:

TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

See <pydantic/pydantic#9612>.

This is a minimal change to limit the scope, instead of risking breaking
changes.
t-muehlberger pushed a commit to AKVorrat/dearmep that referenced this pull request Nov 18, 2024
This should fix errors in combination with Python 3.12, e.g. in build
<https://github.com/AKVorrat/dearmep/actions/runs/11783011436/job/32819299151>,
which had:

TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

See <pydantic/pydantic#9612>.

This is a minimal change to limit the scope, instead of risking breaking
changes.

(cherry picked from commit d46e269)
scy added a commit to AKVorrat/dearmep that referenced this pull request Nov 19, 2024
This should fix errors in combination with Python 3.12, e.g. in build
<https://github.com/AKVorrat/dearmep/actions/runs/11783011436/job/32819299151>,
which had:

TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

See <pydantic/pydantic#9612>.

This is a minimal change to limit the scope, instead of risking breaking
changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0