Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/datamodel_code_generator/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ def dump_all(self, *, multiline: bool = False) -> str:
items = ", ".join(f'"{name}"' for name in name_list)
return f"__all__ = [{items}]"

def get_effective_name(self, from_: str | None, import_: str) -> str:
"""Get the effective name after alias resolution."""
return self.alias.get(from_, {}).get(import_, import_)

def remove_unused(self, used_names: set[str]) -> None:
"""Remove imports not referenced in used_names.

Note: Checks both effective name (after alias) and original name to handle
cases where code may reference either form (e.g., type annotations may use
original name while runtime code uses alias).
"""
unused = [
(from_, import_)
for from_, imports_ in self.items()
for import_ in imports_
if not {self.get_effective_name(from_, import_), import_}.intersection(used_names)
]
for from_, import_ in unused:
alias = self.alias.get(from_, {}).get(import_)
reference_path = next(
(p for p, i in self.reference_paths.items() if i.from_ == from_ and i.import_ == import_),
None,
)
import_obj = Import(from_=from_, import_=import_, alias=alias, reference_path=reference_path)
while self.counter.get((from_, import_), 0) > 0:
self.remove(import_obj)


IMPORT_ANNOTATED = Import.from_full_path("typing.Annotated")
IMPORT_ANY = Import.from_full_path("typing.Any")
Expand Down
4 changes: 4 additions & 0 deletions src/datamodel_code_generator/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ class DataModel(TemplateBase, Nullable, ABC): # noqa: PLR0904
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = ()
IS_ALIAS: ClassVar[bool] = False
SUPPORTS_GENERIC_BASE_CLASS: ClassVar[bool] = True
SUPPORTS_DISCRIMINATOR: ClassVar[bool] = False
SUPPORTS_FIELD_RENAMING: ClassVar[bool] = False
SUPPORTS_WRAPPED_DEFAULT: ClassVar[bool] = False
SUPPORTS_KW_ONLY: ClassVar[bool] = False
has_forward_reference: bool = False

def __init__( # noqa: PLR0913
Expand Down
2 changes: 2 additions & 0 deletions src/datamodel_code_generator/model/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class DataClass(DataModel):

TEMPLATE_FILE_PATH: ClassVar[str] = "dataclass.jinja2"
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = (IMPORT_DATACLASS,)
SUPPORTS_DISCRIMINATOR: ClassVar[bool] = True
SUPPORTS_KW_ONLY: ClassVar[bool] = True

def __init__( # noqa: PLR0913
self,
Expand Down
1 change: 1 addition & 0 deletions src/datamodel_code_generator/model/msgspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class Struct(DataModel):
BASE_CLASS_NAME: ClassVar[str] = "Struct"
BASE_CLASS_ALIAS: ClassVar[str] = "_Struct"
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = ()
SUPPORTS_DISCRIMINATOR: ClassVar[bool] = True
CONFIG_MAPPING: ClassVar[dict[tuple[str, Any], tuple[str, Any] | None]] = {
("allow_mutation", False): ("frozen", True),
("extra_fields", "forbid"): ("forbid_unknown_fields", True),
Expand Down
1 change: 1 addition & 0 deletions src/datamodel_code_generator/model/pydantic/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class BaseModel(BaseModelBase):

TEMPLATE_FILE_PATH: ClassVar[str] = "pydantic/BaseModel.jinja2"
BASE_CLASS: ClassVar[str] = "pydantic.BaseModel"
SUPPORTS_DISCRIMINATOR: ClassVar[bool] = True

def __init__( # noqa: PLR0912, PLR0913
self,
Expand Down
3 changes: 3 additions & 0 deletions src/datamodel_code_generator/model/pydantic_v2/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ class BaseModel(BaseModelBase):
BASE_CLASS: ClassVar[str] = "pydantic.BaseModel"
BASE_CLASS_NAME: ClassVar[str] = "BaseModel"
BASE_CLASS_ALIAS: ClassVar[str] = "_BaseModel"
SUPPORTS_DISCRIMINATOR: ClassVar[bool] = True
SUPPORTS_FIELD_RENAMING: ClassVar[bool] = True
SUPPORTS_WRAPPED_DEFAULT: ClassVar[bool] = True
CONFIG_ATTRIBUTES: ClassVar[list[ConfigAttribute]] = [
ConfigAttribute("allow_population_by_field_name", "populate_by_name", False), # noqa: FBT003
ConfigAttribute("populate_by_name", "populate_by_name", False), # noqa: FBT003
Expand Down
Loading
Loading