You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Force type checker to use Pydantic's V1 API instead (#1432)
## Pull Request Checklist
- [x] Fixes#1435
- [x] Tests added
- [X] Documentation/examples added
- [X] [Good commit messages](https://cbea.ms/git-commit/) and/or PR
title
This is a bit tricky, so I'm not 100% sure if I got this right.
This is the my current understanding of the situation, as described in
this previous [bug
report](#984 (comment)).
- Hera supports pretty much any version of Pydantic from `>=1.7,<3.0`.
- Pydantic V1 and V2 has different APIs and Hera handles this
differently at runtime fine.
- For static type checkers, we need to pick one at type check time using
`typing.TYPE_CHECKING`. This is necessary for both external type
checkers like pyright and editor tooling.
- The current type checker defaults to V2 APIs, even though Hera models
use V1 under the hood.
- This leads to wrong types and editor hints for methods like
`wf.dict()`.
For example, pylance would report this warning in vscode, but it's
actually wrong.
```
The method "dict" in class "BaseModel" is deprecated
The `dict` method is deprecated; use `model_dump` instead.
```
Because Hera models use v1 API, `dict()` is not deprecated. If the user
switch to `model_dump` as prescribed by the error, this would fail at
runtime since `model_dump` doesn't actually exist on v1 models.
I think the simplest solution here is to switch to v1 types explicitly?
Since this is only used for Hera's base classes, there shouldn't be any
impact elsewhere?
## Test Plan
I can think of a few different things to test with both v1 and v2
version of pydantic.
- [X] Make sure type checks pass, which I guess is covered by this CI.
- [X] The [Original
PR](https://github.com/argoproj-labs/hera/pull/950/files) added these
conditional imports to make sure vscode can show inline hints for the
constructors like WorkflowTemplate. Make sure this is OK in vscode.
- [X] Make sure `dict()` doesn't show a warning.
- ✅ I validated these 3 with my work project that's using Pydantic V2.
- ✅ I validated v1 behaviour with this [tiny project in a
gist](https://gist.github.com/jaseemabid/63e26ba25cd3e346d57ca9df791e3565).
Attaching a vscode screenshot as well.
<img width="772" alt="image"
src="https://github.com/user-attachments/assets/955baacc-ce63-41fd-9339-b81e471284ee"
/>
---------
Signed-off-by: Jaseem Abid <[email protected]>
Co-authored-by: Elliot Gunton <[email protected]>
0 commit comments