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

Pin date-time (no import of AwareDatetime) #26

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions dev_requirements/requirements-formatting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ isort==5.12.0
# via -r dev_requirements/requirements-formatting.in
mypy-extensions==1.0.0
# via black
packaging==23.0
packaging==23.2
# via black
pathspec==0.11.0
pathspec==0.11.2
# via black
platformdirs==3.1.0
platformdirs==3.11.0
# via black
2 changes: 1 addition & 1 deletion dev_requirements/requirements-packaging.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mdurl==0.1.2
# via markdown-it-py
more-itertools==9.1.0
# via jaraco-classes
packaging==23.1
packaging==23.2
# via build
pkginfo==1.9.6
# via twine
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile '.\requirements.in'
# pip-compile requirements.in
#
annotated-types==0.6.0
# via pydantic
Expand All @@ -14,6 +14,8 @@ click==8.1.7
# via
# -r requirements.in
# black
colorama==0.4.6
# via click
datamodel-code-generator==0.25.1
# via -r requirements.in
dnspython==2.4.2
Expand Down
2 changes: 1 addition & 1 deletion src/bo4e_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def generate_bo4e_schemas(
for relative_file_path, file_content in file_contents.items():
file_path = output_directory / relative_file_path
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(file_content)
file_path.write_text(file_content, "utf-8")
print(f"Created {file_path}")

print("Done.")
Expand Down
21 changes: 18 additions & 3 deletions src/bo4e_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"""
import re
from pathlib import Path
from typing import Tuple
from typing import Sequence, Tuple, Type

import datamodel_code_generator.parser.base
import datamodel_code_generator.reference
from datamodel_code_generator import DataModelType, PythonVersion
from datamodel_code_generator.imports import IMPORT_DATETIME
from datamodel_code_generator.model import DataModelSet, get_data_model_types
from datamodel_code_generator.model.enum import Enum as _Enum
from datamodel_code_generator.parser.jsonschema import JsonSchemaParser
from datamodel_code_generator.types import DataType, StrictTypes, Types

from bo4e_generator.schema import SchemaMetadata

Expand Down Expand Up @@ -52,11 +54,25 @@ class BO4EDataModel(data_model_types.data_model): # type: ignore[name-defined]
setattr(_Enum, "module_path", _module_path)
setattr(_Enum, "module_name", _module_name)

class BO4EDataTypeManager(data_model_types.data_type_manager): # type: ignore[name-defined]
"""Override the data type manager to use create the namespace. -> ensures desired date-time type"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"""Override the data type manager to use create the namespace. -> ensures desired date-time type"""
"""
Override the data type manager to prevent the code generator from using the `AwareDateTime` type
featured in pydantic v2. Instead, the standard datetime type will be used.
"""

Copy link
Collaborator Author

@DeltaDaniel DeltaDaniel Dec 11, 2023

Choose a reason for hiding this comment

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


def type_map_factory(
self,
data_type: Type[DataType],
strict_types: Sequence[StrictTypes],
pattern_key: str,
) -> dict[Types, DataType]:
"""overwrite the AwareDatetime import"""
result = super().type_map_factory(data_type, strict_types, pattern_key)
result[Types.date_time] = data_type.from_import(IMPORT_DATETIME)
return result

return DataModelSet(
data_model=BO4EDataModel,
root_model=data_model_types.root_model,
field_model=data_model_types.field_model,
data_type_manager=data_model_types.data_type_manager,
data_type_manager=BO4EDataTypeManager,
dump_resolve_reference_action=data_model_types.dump_resolve_reference_action,
known_third_party=data_model_types.known_third_party,
)
Expand Down Expand Up @@ -164,7 +180,6 @@ def parse_bo4e_schemas(
namespace=namespace,
)
monkey_patch_relative_import()

parser = JsonSchemaParser(
input_directory,
data_model_type=data_model_types.data_model,
Expand Down