Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing constr import issue #1609

Merged
merged 7 commits into from
Oct 7, 2023
Merged

Fix missing constr import issue #1609

merged 7 commits into from
Oct 7, 2023

Conversation

koxudaxi
Copy link
Owner

@koxudaxi koxudaxi commented Oct 6, 2023

Closes: #1495

@koxudaxi
Copy link
Owner Author

koxudaxi commented Oct 6, 2023

@indrat
Could you please review it? if you have the time.

@codecov
Copy link

codecov bot commented Oct 6, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (e9b6edf) 100.00% compared to head (9ea165f) 100.00%.
Report is 738 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #1609     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           11        34     +23     
  Lines         1020      3745   +2725     
  Branches       201       877    +676     
===========================================
+ Hits          1020      3745   +2725     
Flag Coverage Δ
unittests 99.65% <99.60%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
datamodel_code_generator/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/__main__.py 100.00% <100.00%> (ø)
datamodel_code_generator/arguments.py 100.00% <100.00%> (ø)
datamodel_code_generator/format.py 100.00% <100.00%> (ø)
datamodel_code_generator/http.py 100.00% <100.00%> (ø)
datamodel_code_generator/imports.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/base.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/dataclass.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/enum.py 100.00% <100.00%> (ø)
... and 24 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -4,7 +4,7 @@

from __future__ import annotations

from msgspec import Struct
from msgspec import Meta, Struct
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@indrat
I think the PR fixes the issue too #1607

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does fix the missing constr import and does appear to fix a similar issue for the msgspec output but not #1607 specifically. #1607 isn't using --collapse-root-models so is ending up with the root model in common.py without the Meta import.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for checking the pattern.
OK, I will fix the issue in another PR.

@indrat
Copy link
Contributor

indrat commented Oct 6, 2023

Testing this with the msgspec output and --target-python 3.8 has an issue with the isort phase where the generated code for common.py has a from typing_extensions import but doesn't import anything:

from __future__ import annotations
from typing import Any
from typing_extensions import 

Model = Any

and thus the isort call fails with:

› datamodel-codegen --input root_test --input-file-type jsonschema --output output_test --disable-timestamp --enable-version-header --collapse-root-models --output-model-type msgspec.Struct --target-python 3.8
Traceback (most recent call last):
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/datamodel_code_generator/__main__.py", line 388, in main
    generate(
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/datamodel_code_generator/__init__.py", line 435, in generate
    results = parser.parse()
              ^^^^^^^^^^^^^^
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/datamodel_code_generator/parser/base.py", line 1183, in parse
    body = code_formatter.format_code(body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/datamodel_code_generator/format.py", line 164, in format_code
    code = self.apply_isort(code)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/datamodel_code_generator/format.py", line 193, in apply_isort
    return isort.code(code, config=self.isort_config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/venv/lib/python3.11/site-packages/isort/api.py", line 92, in sort_code_string
    sort_stream(
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/venv/lib/python3.11/site-packages/isort/api.py", line 210, in sort_stream
    changed = core.process(
              ^^^^^^^^^^^^^
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/venv/lib/python3.11/site-packages/isort/core.py", line 422, in process
    parsed_content = parse.file_contents(import_section, config=config)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/indra/projects/data-model-code-gen/datamodel-code-generator/venv/lib/python3.11/site-packages/isort/parse.py", line 522, in file_contents
    if "," in import_string.split(just_imports[-1])[-1]:
                                  ~~~~~~~~~~~~^^^^
IndexError: list index out of range

@koxudaxi koxudaxi requested a review from indrat October 7, 2023 02:48
Comment on lines +5848 to +5851
'--output-model-type',
'msgspec.Struct',
'--target-python-version',
'3.8',
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@indrat
I have added the test case.

@indrat
Copy link
Contributor

indrat commented Oct 7, 2023

lgtm 👍

@koxudaxi koxudaxi merged commit 85f1955 into master Oct 7, 2023
74 checks passed
@koxudaxi koxudaxi deleted the fix_missing_constr_import branch October 7, 2023 16:23
@koxudaxi
Copy link
Owner Author

koxudaxi commented Oct 7, 2023

Thank you very much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing constr import when --collapse-root-models and using directory input
2 participants