datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Update deprecated Pydantic v2 methods

Open cruisen opened this issue 11 months ago • 3 comments

The package currently uses several deprecated Pydantic v2 methods that should be updated to their new equivalents:

  1. parse_obj -> model_validate
  2. dict -> model_dump
  3. __fields_set__ -> model_fields_set

These deprecation warnings appear when using the package with Pydantic v2:

  • PydanticDeprecatedSince20: The parse_obj method is deprecated; use model_validate instead.
  • PydanticDeprecatedSince20: The dict method is deprecated; use model_dump instead.
  • PydanticDeprecatedSince20: The fields_set attribute is deprecated, use model_fields_set instead

These methods will be removed in Pydantic v3.0, so they should be updated to ensure future compatibility.

Relevant files:

  • datamodel_code_generator/parser/jsonschema.py

See the Pydantic V2 Migration Guide for more details on the changes.

cruisen avatar Dec 09 '24 21:12 cruisen

@koxudaxi The unit tests for datamodel_code_generator/parser/jsonschema.py also throw deprecation warnings for the same reasons as above. Should these be updated as well or do they need to stay for backwards compatibility with Pydantic V1?

riferg206 avatar Dec 16 '24 14:12 riferg206

Yes, this implementation is designed to support both v1 and v2. For backward compatibility reasons, we need to maintain support for both versions. If this is causing issues for users, we could completely suppress these warnings. Would that be a better approach from a user perspective?

koxudaxi avatar Jan 13 '25 16:01 koxudaxi

We should be able to avoid suppressing warnings, which is preferable as both maintainer and user IMO. The deprecation warnings will still get thrown when the unit tests are run, but that may be unavoidable without adding version handling in the unit tests themselves. What is preferable from a maintainers standpoint? I'm new to open source, but if this seems sensible I can open a new PR with the changes from the one I closed and request to merge.

riferg206 avatar Jan 13 '25 20:01 riferg206