Skip to content

Migrated pydantic v1 API to v2 API for compliance and warning reduction#63

Merged
sjoerdk merged 4 commits intosjoerdk:mainfrom
silvandeleemput:62-upgrade-pydantic-v2
Jan 5, 2026
Merged

Migrated pydantic v1 API to v2 API for compliance and warning reduction#63
sjoerdk merged 4 commits intosjoerdk:mainfrom
silvandeleemput:62-upgrade-pydantic-v2

Conversation

@silvandeleemput
Copy link
Contributor

@silvandeleemput silvandeleemput commented Dec 24, 2025

Closes #62 Closes #64

This PR migrates the codebase to use the Pydantic v2 API instead of the deprecated v1 API.
This PR almost removes all of the Pydantic-related deprecation warnings, while still passing all the tests.

The steps that I took for the code changes:

  • Installed a development virtual environment with all the dev dependencies and bump_pydantic
  • I ran the bump_pydantic tool, which solved some issues, but did not solve all the issues
  • I went through all the pydantic warnings, while running pytests, replacing the deprecated V1 api calls with V2 api calls.
  • I re-added the comments removed by bump_pandantic

The only remaining warning is related to the following code.

@model_validator(mode="after")
def min_max_study_date_xor(cls, values): # noqa: B902, N805
"""Min and max should both be given or both be empty"""
min_date = values.min_study_date
max_date = values.max_study_date
if min_date and not max_date:
raise ValueError(
f"min_study_date parameter was passed"
f"({min_date}), "
f"but max_study_date was not. Both need to be given"
)
elif max_date and not min_date:
raise ValueError(
f"max_study_date parameter was passed ({max_date}), "
f"but min_study_date was not. Both need to be given"
)
return values

The warning is:

D:\workspace\dicomtrolley\dicomtrolley\mint.py:164: PydanticDeprecatedSince212: Using `@model_validator` with mode='after' on a classmethod is deprecated. Instead, use an instance method. See the documentation at https://docs.pydantic.dev/2.12/concepts/validators/#model-after-validator. Deprecated in Pydantic V2.12 to be removed in V3.0.

I think this can be easily solved by changing the method to an instance method instead of a class method. Which seems a better fit to me based on my understanding of Pydantic. I've added the change, but please let me know if it should remain a class method for some reason, @sjoerdk.

@sjoerdk
Copy link
Owner

sjoerdk commented Dec 24, 2025

@silvandeleemput thanks for the work!
That migration has been on my to do list for a long time.

It's fine to make those methods instance methods. If all the tests still run we should be fine.

@sjoerdk
Copy link
Owner

sjoerdk commented Dec 24, 2025

This is a major change to dicomtrolley though. It will break older (maybe too old) applications.

@silvandeleemput could you do a major version bump as described here?

https://dicomtrolley.readthedocs.io/en/stable/contributing/#incrementing-the-version-number

Copy link
Owner

@sjoerdk sjoerdk left a comment

Choose a reason for hiding this comment

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

Looks good!
To fix the CI errors look at #64

And please bump the major version as in https://dicomtrolley.readthedocs.io/en/stable/contributing/#incrementing-the-version-number

I can't do this myself as I'm on my phone and making Christmas dinner

@silvandeleemput
Copy link
Contributor Author

@sjoerdk Thank you for your timely reply!
I bumped the major version from 3.3.0 -> 4.0.0 in the pyproject.toml and updated the history.md
I think I solved the flake8 and the black issues using the pre-commit hooks, but let's see what the CI says, my environment is suboptimal for this.

@sjoerdk
Copy link
Owner

sjoerdk commented Jan 5, 2026

Thanks! For some reason github keeps this at status 'blocked, changes requested'. But you already addressed the requested changes. And all tests pass. So I'm bypassing

@sjoerdk sjoerdk merged commit 5017868 into sjoerdk:main Jan 5, 2026
5 checks passed
@silvandeleemput silvandeleemput deleted the 62-upgrade-pydantic-v2 branch January 5, 2026 15:42
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.

Trivial flake8 errors in CI Request to upgrade pydantic v1.0 to pydantic v2.0

2 participants