Commit fa1fc11
fix: move UnionMode import outside TYPE_CHECKING for Pydantic runtime… (#2950)
* fix: move UnionMode import outside TYPE_CHECKING for Pydantic runtime access
The UnionMode type is used in GenerateConfig.union_mode field annotation.
Pydantic needs to access this type at runtime for model validation, but it
was only imported inside TYPE_CHECKING block, causing:
PydanticUserError: GenerateConfig is not fully defined; you should define
UnionMode, then call GenerateConfig.model_rebuild().
This follows the same pattern as other runtime-required imports in this file
(Path, DataModel, DataModelFieldBase, etc.) which have noqa: TC001 comments.
* test: add regression test for UnionMode runtime access
Add test that verifies UnionMode is available at runtime for Pydantic
model_rebuild(). This test runs in a subprocess to ensure a clean namespace
without pytest's import side effects, properly detecting when UnionMode
is incorrectly placed inside TYPE_CHECKING block.
---------
Co-authored-by: Koudai Aono <[email protected]>1 parent 4decf36 commit fa1fc11
File tree
3 files changed
+27
-4
lines changed- src/datamodel_code_generator
- tests
- data/expected/main
- main
3 files changed
+27
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | 55 | | |
59 | 56 | | |
60 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
2062 | 2063 | | |
2063 | 2064 | | |
2064 | 2065 | | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
2065 | 2081 | | |
2066 | 2082 | | |
2067 | 2083 | | |
| |||
0 commit comments