-
-
Notifications
You must be signed in to change notification settings - Fork 449
Fix legacy pydantic extra templates #3602
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
Open
koxudaxi
wants to merge
3
commits into
main
Choose a base branch
from
fix/legacy-pydantic-extra-template
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions
19
...ed/main/jsonschema/additional_properties_schema_with_properties_legacy_custom_template.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,19 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: additional_properties_schema_with_properties.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Dict | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
|
|
||
| class KnownAndExtra(BaseModel): | ||
| model_config = ConfigDict( | ||
| extra='allow', | ||
| ) | ||
| __annotations__ = { | ||
| '__pydantic_extra__': Dict[str, int], | ||
| } | ||
| name: str |
22 changes: 22 additions & 0 deletions
22
tests/data/expected/main/jsonschema/additional_properties_self_ref_legacy_custom_template.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,22 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: additional_properties_self_ref.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Dict | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
|
|
||
| class Node(BaseModel): | ||
| model_config = ConfigDict( | ||
| extra='allow', | ||
| ) | ||
| __annotations__ = { | ||
| '__pydantic_extra__': Dict[str, "Node"], | ||
| } | ||
| name: str | ||
|
|
||
|
|
||
| Node.model_rebuild() |
71 changes: 71 additions & 0 deletions
71
tests/data/templates_pydantic_extra_pre_3593/pydantic_v2/BaseModel.jinja2
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,71 @@ | ||
| {% for decorator in decorators -%} | ||
| {{ decorator }} | ||
| {% endfor -%} | ||
| class {{ class_name }}({%- if schema_runtime_validation_use_base -%}{{ schema_runtime_validation_base_class_name }}{% if base_class != 'BaseModel' %}, {{ base_class }}{% endif %}{%- else -%}{{ base_class }}{%- endif -%}):{% if comment is defined %} # {{ comment }}{% endif %} | ||
| {%- if description %} | ||
| {{ description }} | ||
| {%- endif %} | ||
| {%- if not fields and not description and not config and not class_body_lines and not schema_runtime_validation %} | ||
| pass | ||
| {%- endif %} | ||
| {%- if config %} | ||
| {%- filter indent(4) %} | ||
| {% include 'ConfigDict.jinja2' %} | ||
| {%- endfilter %} | ||
| {%- endif %} | ||
| {%- for line in class_body_lines %} | ||
| {{ line }} | ||
| {%- endfor %} | ||
| {%- include 'schema_runtime_validation.jinja2' %} | ||
| {%- for field in fields %} | ||
| {%- if not field.use_pydantic_extra_annotation_assignment and not field.annotated and field.field %} | ||
| {{ field.name }}: {{ field.type_hint }} = {{ field.field }} | ||
| {%- elif not field.use_pydantic_extra_annotation_assignment %} | ||
| {%- if field.annotated %} | ||
| {{ field.name }}: {{ field.annotated }} | ||
| {%- else %} | ||
| {{ field.name }}: {{ field.type_hint }} | ||
| {%- endif %} | ||
| {%- if not field.has_default_factory_in_field and (not field.required or field.use_default_with_required) and (field.represented_default != 'None' or not field.strip_default_none or field.data_type.is_optional) | ||
| %} = {{ field.represented_default }} | ||
| {%- endif -%} | ||
| {%- endif %} | ||
| {%- if field.docstring %} | ||
| {{ field.docstring }} | ||
| {%- if field.use_inline_field_description and not loop.last %} | ||
|
|
||
| {% endif %} | ||
| {%- elif field.inline_field_docstring %} | ||
| {{ field.inline_field_docstring }} | ||
| {%- if not loop.last %} | ||
|
|
||
| {% endif %} | ||
| {%- endif %} | ||
| {%- for method in methods -%} | ||
| {{ method }} | ||
| {%- endfor -%} | ||
| {%- endfor -%} | ||
| {%- if prepared_validators %} | ||
| {% for v in prepared_validators %} | ||
|
|
||
| @field_validator({{ v.fields_str }}, {{ v.mode_str }}) | ||
| @classmethod | ||
| {%- if v.mode == 'wrap' %} | ||
| def {{ v.method_name }}(cls, v: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo) -> Any: | ||
| return {{ v.function_name }}(v, handler, info) | ||
| {%- elif v.mode == 'plain' %} | ||
| def {{ v.method_name }}(cls, v: Any) -> Any: | ||
| return {{ v.function_name }}(v) | ||
| {%- else %} | ||
| def {{ v.method_name }}(cls, v: Any, info: ValidationInfo) -> Any: | ||
| return {{ v.function_name }}(v, info) | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- endif %} | ||
| {%- for field in fields %} | ||
| {%- if field.use_pydantic_extra_annotation_assignment %} | ||
|
|
||
| {{ class_name }}.__annotations__['__pydantic_extra__'] = {{ field.pydantic_extra_type_hint }} | ||
| {{ class_name }}.model_rebuild(force=True) | ||
| {%- endif %} | ||
| {%- endfor %} |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.