Commit d7018ec
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 <me@jabid.in>
Co-authored-by: Elliot Gunton <elliotgunton@gmail.com>1 parent 1ab6d23 commit d7018ec
File tree
4 files changed
+41
-28
lines changed- src/hera
- workflows/io
- tests/test_unit
4 files changed
+41
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
| 30 | + | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
38 | 51 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
| 52 | + | |
| 53 | + | |
48 | 54 | | |
49 | 55 | | |
50 | 56 | | |
| |||
55 | 61 | | |
56 | 62 | | |
57 | 63 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | 64 | | |
70 | 65 | | |
71 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
154 | 172 | | |
155 | 173 | | |
156 | 174 | | |
| |||
0 commit comments