-
-
Notifications
You must be signed in to change notification settings - Fork 437
fix: Improve state management in Imports, DataType, and DataModel classes #2705
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
935384a
fix: Enhance deep copy functionality and improve import management in…
koxudaxi 8f8a4de
fix: Import Reference in dataclass.py to resolve missing dependency i…
koxudaxi 1c7df54
fix: Resolve import issues and enhance deep copy functionality in cor…
koxudaxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/data/expected/main/jsonschema/reuse_scope_tree_dataclass_frozen/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: reuse_scope_tree_dataclass | ||
| # timestamp: 2019-07-26T00:00:00+00:00 |
20 changes: 20 additions & 0 deletions
20
tests/data/expected/main/jsonschema/reuse_scope_tree_dataclass_frozen/schema_a.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: reuse_scope_tree_dataclass | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Optional | ||
|
|
||
| from .shared import SharedModel as SharedModel_1 | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class SharedModel(SharedModel_1): | ||
| pass | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Model: | ||
| data: Optional[SharedModel] = None |
15 changes: 15 additions & 0 deletions
15
tests/data/expected/main/jsonschema/reuse_scope_tree_dataclass_frozen/schema_b.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: schema_b.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Optional | ||
|
|
||
| from . import shared | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Model: | ||
| info: Optional[shared.SharedModel] = None |
14 changes: 14 additions & 0 deletions
14
tests/data/expected/main/jsonschema/reuse_scope_tree_dataclass_frozen/shared.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: shared.py | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Optional | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class SharedModel: | ||
| id: Optional[int] = None | ||
| name: Optional[str] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Cyclic import Note
Copilot Autofix
AI 5 months ago
In general, cycles caused by type-like utilities can be fixed by making the dependency one-way at runtime: either move shared types into a more central module, or ensure one side only imports the other for type checking (using
TYPE_CHECKINGandfrom __future__ import annotations) or local, function‑scope imports.The single best minimal fix here, without changing existing functionality, is to stop importing
Referenceat module level and instead import it only for typing. Because this file already usesfrom __future__ import annotations, we can safely annotate with"Reference"as a string and importReferenceonly inside theTYPE_CHECKINGblock. That way, the runtime import graph no longer includesdatamodel_code_generator.referencefromdataclass.py, breaking the cycle, while static type checkers still see the correct type. This does not change runtime behavior:Referenceis only needed for type annotations and as a constructor call, and with postponed evaluation, using the forward‑referenced annotation as a string is sufficient.Concretely in
src/datamodel_code_generator/model/dataclass.py:from datamodel_code_generator.reference import Referenceimport.if TYPE_CHECKING:block, addfrom datamodel_code_generator.reference import Reference.Referenceto use a forward reference string"Reference"instead of the bare name, so they remain valid even if the name is not bound at runtime (although withfrom __future__ import annotations, this is largely for clarity and robustness).