-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[PYTHON] generate code based on pydantic v2 #16685
Conversation
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.
Running the tests in samples/openapi3/client/petstore/python-aiohttp
goes from:
148 passed, 2 skipped, 2100 warnings
to:
148 passed, 2 skipped, 60 warnings
and I don't see any Pydantic v2 warnings anymore 👏
FYI @taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @arun-nalla (2019/11) @krjakbrjak (2023/02) |
tested locally and all those pydantic warnings are gone 👍 |
I am receiving It's caused by # raise errors for additional fields in the input
for _key in obj.keys():
> if _key not in cls.__properties:
E TypeError: argument of type 'ModelPrivateAttr' is not iterable Whole snippet __properties = ["sessionID"]
@classmethod
def from_dict(cls, obj: dict) -> Session:
"""Create an instance of Session from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return Session.model_validate(obj)
# raise errors for additional fields in the input
for _key in obj.keys():
if _key not in cls.__properties:
raise ValueError(
"Error due to additional fields (not defined in Session) in the input: "
+ obj
)
_obj = Session.model_validate(
{
"sessionID": obj.get("sessionID"),
}
)
return _obj It worked in #16655 |
@jakubno do you have an OpenAPI example where this happens? This was changed in #16624 but I think we missed a branch that was not covered by the tests. |
I will create minimal example and create issue |
Great, thanks 👍 If that helps, it seems it has something to do with "additional properties": this is the part in the code generator that may not be covered by the tests. |
Perhaps |
Yep, that's what I did in the branch that was covered by the tests 👍 |
If you test this, you need to set the |
* [python] replace validator with field_validator * [python] replace parse_obj with model_validate * [python] replace dict with model_dump * [python] replace construct with model_construct * [python] replace __fields_set__ with model_fields_set * [python] replace __fields_set__ in the test cases with model_fields_set * [python] replace validate_arguments with validate_call * [python] replace max_items, min_items with max_length, min_length * [python] replace Config class with model_config * [python] replace allow_population_by_field_name with populate_by_name * [python] remove {{{classname}}}_ONE_OF_SCHEMAS * [python] update test cases * [python] update samples * [python] fix typos in test cases
NOTE: for users who still prefer using pydantic v1 in v7.1.0 release, they can use the |
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)