-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix missing recursive_guard parameter in Pydantic v1 for python 3.12.4+ #9611
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
Conversation
CodSpeed Performance ReportMerging #9611 will not alter performanceComparing Summary
|
pydantic/v1/typing.py
Outdated
else: | ||
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any: | ||
# For 3.12.4+ provide a default `recursive_guard` to resolve: | ||
# TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard' | ||
return cast(Any, type_)._evaluate(globalns, localns, set(), recursive_guard=set()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I made a mistake when i glanced at the code earlier. That's what i get when i do things in a rush.
Python 3.12.4 has this commit:
vfazio@Zephyrus:~/development/cpython$ git describe --contains 5430f614371530aab1178b3b610add4bf54c383e
v3.12.4~194
Which is: python/cpython#118104
Which changed the function signature of ForwardRef._evaluate
to force recursive_guard
to be a kwarg
def _evaluate(self, globalns, localns, type_params=None, *, recursive_guard):
>>> inspect.signature(typing.ForwardRef._evaluate).parameters['recursive_guard'].kind
<_ParameterKind.KEYWORD_ONLY: 3>
The fixup should maybe be:
if sys.version_info < (3, 9):
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
return type_._evaluate(globalns, localns)
else:
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
# Even though it is the right signature for python 3.9, mypy complains with
# `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast...
# Note 3.13/3.12.4+ made `recursive_guard` a kwarg, so name it explicitly to avoid:
# TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'
return cast(Any, type_)._evaluate(globalns, localns, recursive_guard=set())
The syntax should work on previous versions (pre 3.12.4/3.13) and also work for the new kwarg status.
The extra set()
is unnecessary and is actually overwriting the default value for type_params
.
Anyway, totally untested, but my thoughts. I am not a maintainer, so this should also be taken with a modest amount of skepticism
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I made a mistake when i glanced at the code earlier. That's what i get when i do things in a rush.
Python 3.12.4 has this commit:
vfazio@Zephyrus:~/development/cpython$ git describe --contains 5430f614371530aab1178b3b610add4bf54c383e v3.12.4~194
Which is: python/cpython#118104
Which changed the function signature of
ForwardRef._evaluate
to forcerecursive_guard
to be a kwargdef _evaluate(self, globalns, localns, type_params=None, *, recursive_guard):>>> inspect.signature(typing.ForwardRef._evaluate).parameters['recursive_guard'].kind <_ParameterKind.KEYWORD_ONLY: 3>The fixup should maybe be:
if sys.version_info < (3, 9): def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any: return type_._evaluate(globalns, localns) else: def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any: # Even though it is the right signature for python 3.9, mypy complains with # `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast... # Note 3.13/3.12.4+ made `recursive_guard` a kwarg, so name it explicitly to avoid: # TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard' return cast(Any, type_)._evaluate(globalns, localns, recursive_guard=set())
The syntax should work on previous versions (pre 3.12.4/3.13) and also work for the new kwarg status.
The extra
set()
is unnecessary and is actually overwriting the default value fortype_params
.Anyway, totally untested, but my thoughts
Is it just that I need to run tests on all python versions to make sure it's correct? Can you guide me, because I really want to fix this error, thank you.
This change looks great. Could you please create a branch from |
If this hasn't been done by tomorrow AM, I'll make a PR. This issue is preventing us from taking 3.12.4 so I'd like to see this fixed. |
I will do it now. This PR will be closed because I will open a new PR on branch 1.10.X-fixes. Thanks to @sydney-runkle and @vfazio |
HI all, I'm just running into this error: File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pydantic/v1/typing.py", line 520, in update_field_forward_refs pydantic version is 3.6.1 and my Python version is 3.12.4. How can I fix this error? SOS~~ THANKS |
Change Summary
I add args recursive guard=set() when calling evaluate function because in python 3.12 they don't add default value for this params. You can see that here.
While waiting for this merge request to be accepted, you can quickly fix it by using python version 3.12.3
Related issue number
fix #9609 and fix #9607
Checklist
Skip change file check