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

patch pydantic v1.10.9 conflict with py3.12 #2014

Merged

Conversation

luca-knaack-webcom
Copy link
Contributor

There are errors in pipelines that run the testing configuration python=3.12 and pydantic=1.10.9. The error is something like:

| ________________ ERROR collecting tests/parser/test_openapi.py _________________
| tests/parser/test_openapi.py:10: in <module>
|     from datamodel_code_generator import OpenAPIScope, PythonVersion
| datamodel_code_generator/__init__.py:35: in <module>
|     from datamodel_code_generator.parser.base import Parser
| datamodel_code_generator/parser/base.py:37: in <module>
|     from datamodel_code_generator.model import pydantic as pydantic_model
| datamodel_code_generator/model/__init__.py:5: in <module>
|     from ..types import DataTypeManager as DataTypeManagerABC
| datamodel_code_generator/types.py:240: in <module>
|     class DataType(_BaseModel):
| .venv/lib/python3.12/site-packages/pydantic/main.py:286: in __new__
|     cls.__try_update_forward_refs__()
| .venv/lib/python3.12/site-packages/pydantic/main.py:808: in __try_update_forward_refs__
|     update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))
| .venv/lib/python3.12/site-packages/pydantic/typing.py:554: in update_model_forward_refs
|     update_field_forward_refs(f, globalns=globalns, localns=localns)
| .venv/lib/python3.12/site-packages/pydantic/typing.py:520: in update_field_forward_refs
|     field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
| .venv/lib/python3.12/site-packages/pydantic/typing.py:66: in evaluate_forwardref
|     return cast(Any, type_)._evaluate(globalns, localns, set())
| E   TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

and results in a massive amount of errors like:

ERROR tests/test_format.py - TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

To patch the missing recursive_guard keyword argument, I created a pydantic_patch file that gets imported early in datamodel_code_generator.__init__.py and that patches the underlying evaluate_forwardref function.

This PR is should fix most of the broken tests in the pipelines for pull requests #2013, #2009 and #2008.

Copy link

codecov bot commented Jun 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@28be37d). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2014   +/-   ##
=======================================
  Coverage        ?   98.81%           
=======================================
  Files           ?       38           
  Lines           ?     4215           
  Branches        ?      981           
=======================================
  Hits            ?     4165           
  Misses          ?       31           
  Partials        ?       19           
Flag Coverage Δ
unittests 98.48% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

luca-knaack-webcom and others added 4 commits June 25, 2024 17:26

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
for more information, see https://pre-commit.ci

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…' into fix/pydantic-1-10-9-with-py-3-12
@luca-knaack-webcom
Copy link
Contributor Author

While there is still one failing pipeline, this is the godspeed-benchmark, that isn't able to run on any PR's pipeline.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Copy link

codspeed-hq bot commented Jun 26, 2024

CodSpeed Performance Report

Merging #2014 will not alter performance

Comparing luca-knaack-webcom:fix/pydantic-1-10-9-with-py-3-12 (611d070) with main (28be37d)

Summary

✅ 29 untouched benchmarks

@luca-knaack-webcom
Copy link
Contributor Author

@koxudaxi, I had to change the codspeed-benchmark action to always install the dependencies, as the cached virtual environment seems to lack the pytest-xdist package. Once the cache is refreshed, the commented out line could be reintroduced.

)


if '3.12' in sys.version: # pragma: no cover
Copy link
Owner

Choose a reason for hiding this comment

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

Is this a problem that also occurs in, for example, Python 3.13?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked this now with python v3.13.0-beta.2 and pydantic v2.8.0b1 (earliest version supporting python 3.13) and the problem does not occur. It seems like the broken ForwardRef is really only happening for python 3.12

Copy link
Contributor

@cclauss cclauss Jul 1, 2024

Choose a reason for hiding this comment

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

@koxudaxi
Copy link
Owner

@cclauss
Thank you for spending so much time on this difficult issue.
I was commenting on the code, but I am concerned about the support for other versions, but I don't think it is a problem now, so I will merge it.
Thank you very much.

@koxudaxi koxudaxi merged commit 84d879b into koxudaxi:main Jun 26, 2024
92 checks passed
@koxudaxi
Copy link
Owner

For performance reasons, you may also want to include a version check on the pydantic side.

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.

None yet

3 participants