Skip to content

Commit 2ea6244

Browse files
authored
Fix incorrect relative imports with --use-exact-imports and --collapse-root-models (#2996)
* Fix incorrect relative imports with --use-exact-imports and --collapse-root-models * Generate root __init__.py for multi-directory output packages
1 parent 34b7d29 commit 2ea6244

12 files changed

Lines changed: 127 additions & 0 deletions

File tree

src/datamodel_code_generator/parser/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,8 @@ def __collapse_root_models( # noqa: PLR0912, PLR0914, PLR0915
20792079
if d.reference is None:
20802080
continue
20812081
from_, import_ = full_path = relative(model.module_name, d.full_name)
2082+
if imports.use_exact:
2083+
from_, import_ = full_path = exact_import(from_, import_, d.reference.short_name)
20822084
if from_ and import_:
20832085
alias = scoped_model_resolver.add(full_path, import_)
20842086
d.alias = (
@@ -3393,6 +3395,12 @@ def parse( # noqa: PLR0913, PLR0914, PLR0917
33933395

33943396
self._finalize_modules(contexts, unused_models, model_to_module_models, module_to_import)
33953397

3398+
root_init: ModulePath = ("__init__.py",)
3399+
if root_init not in results:
3400+
top_level_dirs = {k[0] for k in results if len(k) >= 2} # noqa: PLR2004
3401+
if len(top_level_dirs) > 1:
3402+
results[root_init] = Result(body="")
3403+
33963404
future_imports = self.imports.extract_future()
33973405
future_imports_str = str(future_imports)
33983406

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by datamodel-codegen:
2+
# filename: exact_imports_collapse_root_models
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# generated by datamodel-codegen:
2+
# filename: data/ReflectionSetData.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import BaseModel, Field
7+
8+
from ..datatypes.BarStats import BarStats
9+
from ..datatypes.FooStats import FooStats
10+
11+
12+
class ReflectionSetData(BaseModel):
13+
stats: FooStats | None = None
14+
stats_list: list[FooStats] | None = None
15+
stats_collection: list[BarStats] | None = Field(None, title='BarStatsList')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by datamodel-codegen:
2+
# filename: exact_imports_collapse_root_models
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated by datamodel-codegen:
2+
# filename: datatypes/BarStats.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import BaseModel
7+
8+
9+
class BarStats(BaseModel):
10+
value: float | None = None
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# generated by datamodel-codegen:
2+
# filename: datatypes/FooStats.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import BaseModel
7+
8+
9+
class FooStats(BaseModel):
10+
count: int | None = None
11+
average: float | None = None
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by datamodel-codegen:
2+
# filename: exact_imports_collapse_root_models
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "ReflectionSetData",
4+
"type": "object",
5+
"properties": {
6+
"stats": {
7+
"allOf": [
8+
{
9+
"$ref": "../datatypes/FooStats.json"
10+
}
11+
]
12+
},
13+
"stats_list": {
14+
"type": "array",
15+
"items": {
16+
"$ref": "../datatypes/FooStats.json"
17+
}
18+
},
19+
"stats_collection": {
20+
"$ref": "../datatypes/BarStatsList.json"
21+
}
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "BarStats",
4+
"type": "object",
5+
"properties": {
6+
"value": {
7+
"type": "number"
8+
}
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "BarStatsList",
4+
"type": "array",
5+
"items": {
6+
"$ref": "BarStats.json"
7+
}
8+
}

0 commit comments

Comments
 (0)