Skip to content

Commit

Permalink
REF: Support pydantic v2 with pydantic.v1
Browse files Browse the repository at this point in the history
  • Loading branch information
westurner committed Oct 20, 2024
1 parent 1a21f94 commit dced8cf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, List

from pydantic import BaseModel, validator
from pydantic.v1 import BaseModel, validator


class PydanticBase(BaseModel):
Expand Down
18 changes: 9 additions & 9 deletions src/schema_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def extract_fields(self, name: str) -> (List[PydanticField], List[Import]):
if name != field_type:
imports = self.update_imports(
imports,
class_path=f"{PACKAGE_NAME}.{field_type}",
class_path=f"{PACKAGE_NAME}.v1.{field_type}",
classes_={field_type},
type="pydantic_field",
)
Expand Down Expand Up @@ -172,7 +172,7 @@ def load_type(self, name: str) -> PydanticClass:
for parent in parents:
imports = self.update_imports(
imports,
class_path=f"{PACKAGE_NAME}.{parent.valid_name}",
class_path=f"{PACKAGE_NAME}.v1.{parent.valid_name}",
classes_={parent.valid_name},
type="parent",
)
Expand All @@ -189,9 +189,9 @@ def load_type(self, name: str) -> PydanticClass:
forward_refs=forward_refs
)

with open(f"{PACKAGE_NAME}/{self.pydantic_classes[name].valid_name}.py", "w") as model_file:
with open(Path(PACKAGE_NAME) / f"{self.pydantic_classes[name].valid_name}.py", "w") as model_file:
with open(
Path(__file__).parent / "templates/model.py.tpl"
Path(__file__).parent / "templates" / "model.py.tpl"
) as template_file:
template = jinja_env.from_string(template_file.read())
template_args = dict(
Expand Down Expand Up @@ -253,12 +253,12 @@ def extract_parents(self, node) -> (List[PydanticClass], list, int):

@staticmethod
def _get_default_imports() -> List[Import]:
return [Import(classes_={"Field"}, classPath="pydantic", type="parent")]
return [Import(classes_={"Field"}, classPath="pydantic.v1", type="parent")]

def write_init(self):
with open(f"{PACKAGE_NAME}/__init__.py", "w") as init_file:
with open(Path(PACKAGE_NAME) / "__init__.py", "w") as init_file:
with open(
Path(__file__).parent / "templates/__init__.py.tpl"
Path(__file__).parent / "templates" / "__init__.py.tpl"
) as template_file:
template = jinja_env.from_string(template_file.read())

Expand All @@ -273,9 +273,9 @@ def write_init(self):
self.write_type_map()

def write_type_map(self):
with open(f"{PACKAGE_NAME}/__types__.py", "w") as type_file:
with open(Path(PACKAGE_NAME) / "__types__.py", "w") as type_file:
with open(
Path(__file__).parent / "templates/__types__.py.tpl"
Path(__file__).parent / "templates" / "__types__.py.tpl"
) as template_file:
template = jinja_env.from_string(template_file.read())

Expand Down
4 changes: 2 additions & 2 deletions src/templates/schema_org_base.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ from datetime import time, datetime, date
from decimal import Decimal
from typing import Any, Optional, ForwardRef, List, Union

from pydantic import BaseModel, Field, StrictBool, AnyUrl, StrictInt, StrictFloat
from pydantic.typing import update_model_forward_refs
from pydantic.v1 import BaseModel, Field, StrictBool, AnyUrl, StrictInt, StrictFloat
from pydantic.v1.typing import update_model_forward_refs

from pydantic_schemaorg.ISO8601.ISO8601Date import ISO8601Date
from pydantic_schemaorg.__types__ import types
Expand Down

0 comments on commit dced8cf

Please sign in to comment.