Skip to content

Commit 9dc9dd4

Browse files
authored
Add multiple --input-model support with inheritance preservation (#2881)
* Add multiple --input-model support with inheritance preservation * Fix lint and coverage issues * Simplify code to improve branch coverage * Extract input-model processing to separate module * Fix PEP8 naming violations and unused parameter * Fix lint error for multi-line import * Add expected_output_not_contains parameter to test helper * Achieve 100% test coverage for input_model feature * Mark pydantic v1 coercion code with pragma no cover * Refactor input_model tests to use expected files with assert_output * Fix expected files for Python version compatibility * Fix import order * Fix PR review issues: duplicate imports, duplicate test, and duplicate union types * Fix union type override for anyOf schemas and remove duplicate test * Clarify test docstrings: input model type tests all output to default Pydantic BaseModel * Remove line comments * Add test for x-python-type with union in anyOf schema * Remove line comments except ignore comments * Remove SKIP_PYTHON_314 and fix Union serialization for Python 3.14 * Refactor duplicate tests using pytest.mark.parametrize * Fix lint and type errors * Regenerate config-types * Fix StrictTypes import in config-types with reuse-foreign strategy - Add _resolve_type_import_from_defs method to resolve imports from $defs entries with x-python-import metadata - Handle Annotated types in _serialize_python_type_full to prevent invalid type serialization with FieldInfo * Use FQN for type arguments in x-python-type serialization Add _full_type_name helper function that generates fully qualified names for type arguments while keeping outer types as short names. This ensures proper import resolution for custom types like StrictTypes. - Add _full_type_name function with Union type handling (| syntax) - Update _serialize_python_type to use _full_type_name for type args - Update expected test file for config_class output * Fix pyright type error in _full_type_name * Add unit tests for 100% patch coverage - Add tests for _simple_type_name edge cases (NoneType, generic types) - Add tests for _full_type_name (string annotation, ForwardRef, typing specials) - Add tests for _serialize_python_type_full with Annotated type - Add tests for _resolve_type_import_from_defs (found, not found, no x-python-import, exception handling) 🤖 Generated with [Claude Code](https://claude.com/claude-code) * Fix lint error and improve test for generic type without args Use list.__class_getitem__(()) to create a GenericAlias with origin but no args, avoiding the ruff UP035 auto-fix that converted typing.List to list. 🤖 Generated with [Claude Code](https://claude.com/claude-code) * Fix variable naming: use 'spec' for find_spec result Renamed misleading variable from 'module' to 'spec' when storing the result of importlib.util.find_spec(), as it returns a ModuleSpec not a module object. 🤖 Generated with [Claude Code](https://claude.com/claude-code) * Add tests for _full_type_name branch coverage - Test builtin type (module='builtins') returns short name - Test collections.abc type returns short name 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 36530b1 commit 9dc9dd4

58 files changed

Lines changed: 2988 additions & 1054 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/datamodel_code_generator/__main__.py

Lines changed: 31 additions & 715 deletions
Large diffs are not rendered by default.

src/datamodel_code_generator/_types/graphql_parser_config_dict.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from datamodel_code_generator.types import DataTypeManager
3131

3232

33-
class GraphQLParserConfigDict(TypedDict):
33+
class ParserConfig(TypedDict):
3434
data_model_type: NotRequired[type[DataModel]]
3535
data_model_root_type: NotRequired[type[DataModel]]
3636
data_type_manager_type: NotRequired[type[DataTypeManager]]
@@ -142,5 +142,8 @@ class GraphQLParserConfigDict(TypedDict):
142142
read_only_write_only_model_type: NotRequired[ReadOnlyWriteOnlyModelType | None]
143143
field_type_collision_strategy: NotRequired[FieldTypeCollisionStrategy | None]
144144
target_pydantic_version: NotRequired[TargetPydanticVersion | None]
145+
146+
147+
class GraphQLParserConfigDict(ParserConfig):
145148
data_model_scalar_type: NotRequired[type[DataModel]]
146149
data_model_union_type: NotRequired[type[DataModel]]

src/datamodel_code_generator/_types/jsonschema_parser_config_dict.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from datamodel_code_generator.types import DataTypeManager
3131

3232

33-
class JSONSchemaParserConfigDict(TypedDict):
33+
class ParserConfig(TypedDict):
3434
data_model_type: NotRequired[type[DataModel]]
3535
data_model_root_type: NotRequired[type[DataModel]]
3636
data_type_manager_type: NotRequired[type[DataTypeManager]]
@@ -142,3 +142,7 @@ class JSONSchemaParserConfigDict(TypedDict):
142142
read_only_write_only_model_type: NotRequired[ReadOnlyWriteOnlyModelType | None]
143143
field_type_collision_strategy: NotRequired[FieldTypeCollisionStrategy | None]
144144
target_pydantic_version: NotRequired[TargetPydanticVersion | None]
145+
146+
147+
class JSONSchemaParserConfigDict(ParserConfig):
148+
pass

src/datamodel_code_generator/_types/openapi_parser_config_dict.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from datamodel_code_generator.types import DataTypeManager
3232

3333

34-
class OpenAPIParserConfigDict(TypedDict):
34+
class ParserConfig(TypedDict):
3535
data_model_type: NotRequired[type[DataModel]]
3636
data_model_root_type: NotRequired[type[DataModel]]
3737
data_type_manager_type: NotRequired[type[DataTypeManager]]
@@ -143,6 +143,13 @@ class OpenAPIParserConfigDict(TypedDict):
143143
read_only_write_only_model_type: NotRequired[ReadOnlyWriteOnlyModelType | None]
144144
field_type_collision_strategy: NotRequired[FieldTypeCollisionStrategy | None]
145145
target_pydantic_version: NotRequired[TargetPydanticVersion | None]
146+
147+
148+
class JSONSchemaParserConfig(ParserConfig):
149+
pass
150+
151+
152+
class OpenAPIParserConfigDict(JSONSchemaParserConfig):
146153
openapi_scopes: NotRequired[list[OpenAPIScope] | None]
147154
include_path_parameters: NotRequired[bool]
148155
use_status_code_in_response_name: NotRequired[bool]

src/datamodel_code_generator/arguments.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ def start_section(self, heading: str | None) -> None:
159159
)
160160
base_options.add_argument(
161161
"--input-model",
162+
action="append",
162163
help="Python import path to a Pydantic v2 model or schema dict "
163164
"(e.g., 'mypackage.module:ClassName' or 'mypackage.schemas:SCHEMA_DICT'). "
165+
"Can be specified multiple times for related models with inheritance. "
164166
"For dict input, --input-file-type is required. "
165167
"Cannot be used with --input or --url.",
166168
metavar="MODULE:NAME",

0 commit comments

Comments
 (0)