Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Nov 5, 2024
1 parent 0fae97b commit 8eb4874
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import fern.ir.resources as ir_types

from fern_python.codegen import AST, LocalClassReference, SourceFile
from fern_python.external_dependencies.pydantic import PydanticVersionCompatibility
from fern_python.pydantic_codegen import PydanticField, PydanticModel

from ..context import PydanticGeneratorContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ def write_example_for_docstring(self, writer: AST.NodeWriter) -> None:

with writer.indent():
writer.write_line("...")

def _get_root_property_name(self) -> str:
return "__root__" if self._model._version == PydanticVersionCompatibility.V1 else "root"
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,13 @@ def add_root_validator(
),
)

def set_root_type_unsafe(
self, root_type: AST.TypeHint, annotation: Optional[AST.Expression] = None
) -> None:
def set_root_type_unsafe(self, root_type: AST.TypeHint, annotation: Optional[AST.Expression] = None) -> None:
if self._version == PydanticVersionCompatibility.Both:
raise RuntimeError("Overriding root types is only available in Pydantic v1 or v2")

if self._root_type is not None:
raise RuntimeError("__root__ was already added")

self.root_type = root_type

root_type_with_annotation = (
Expand All @@ -270,16 +268,15 @@ def set_root_type_unsafe(
else root_type
)

if self._version == PydanticVersionCompatibility.V1:
if self._version == PydanticVersionCompatibility.V1:
self._class_declaration.add_statement(
AST.VariableDeclaration(name="__root__", type_hint=root_type_with_annotation)
)
if self._version == PydanticVersionCompatibility.V2:

if self._version == PydanticVersionCompatibility.V2:
self._class_declaration.add_statement(
AST.VariableDeclaration(name="root", type_hint=root_type_with_annotation)
)


def get_root_type_unsafe(self) -> Optional[AST.TypeHint]:
return self.root_type
Expand Down

0 comments on commit 8eb4874

Please sign in to comment.