Skip to content

Commit

Permalink
Don't generate files without model (#2044)
Browse files Browse the repository at this point in the history
Remove unused imports if there are no models in a module.

Once the unused imports are skipped, don't create the file if there
is nothing left in it, unless its `__init__.py`.
  • Loading branch information
kmichel-aiven authored Aug 6, 2024
1 parent 9c92252 commit 5500c0f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
34 changes: 18 additions & 16 deletions datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,22 +1404,24 @@ class Processed(NamedTuple):

for module, models, init, imports, scoped_model_resolver in processed_models:
result: List[str] = []
if with_import:
result += [str(self.imports), str(imports), '\n']

code = dump_templates(models)
result += [code]

if self.dump_resolve_reference_action is not None:
result += [
'\n',
self.dump_resolve_reference_action(
m.reference.short_name
for m in models
if m.path in require_update_action_models
),
]

if models:
if with_import:
result += [str(self.imports), str(imports), '\n']

code = dump_templates(models)
result += [code]

if self.dump_resolve_reference_action is not None:
result += [
'\n',
self.dump_resolve_reference_action(
m.reference.short_name
for m in models
if m.path in require_update_action_models
),
]
if not result and not init:
continue
body = '\n'.join(result)
if code_formatter:
body = code_formatter.format_code(body)
Expand Down
2 changes: 0 additions & 2 deletions tests/data/expected/main/jsonschema/nested_skip/a/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# generated by datamodel-codegen:
# filename: nested_skip.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# generated by datamodel-codegen:
# filename: nested_skip.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# generated by datamodel-codegen:
# filename: nested_skip.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
4 changes: 4 additions & 0 deletions tests/data/jsonschema/external_collapse/child.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"type": "string"
}
6 changes: 6 additions & 0 deletions tests/data/jsonschema/external_collapse/parent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"properties": {
"item": {"$ref": "child.json"}
}
}
22 changes: 22 additions & 0 deletions tests/main/jsonschema/test_main_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,28 @@ def test_main_jsonschema_multiple_files():
assert result == path.read_text()


@pytest.mark.benchmark
@freeze_time('2019-07-26')
def test_main_jsonschema_no_empty_collapsed_external_model():
with TemporaryDirectory() as output_dir:
output_dir: Path = Path(output_dir) / 'output'
output_dir.mkdir()
return_code: Exit = main(
[
'--input',
str(JSON_SCHEMA_DATA_PATH / 'external_collapse'),
'--output',
str(output_dir),
'--input-file-type',
'jsonschema',
'--collapse-root-models',
]
)
assert return_code == Exit.OK
assert not (output_dir / 'child.py').exists()
assert (output_dir / '__init__.py').exists()


@pytest.mark.parametrize(
'output_model,expected_output',
[
Expand Down

0 comments on commit 5500c0f

Please sign in to comment.