Skip to content

Commit

Permalink
fix(python): respect serialization with more types and dump JSON corr…
Browse files Browse the repository at this point in the history
…ectly (#4694)
  • Loading branch information
armandobelardo authored Sep 20, 2024
1 parent 99045b3 commit 539cfc4
Show file tree
Hide file tree
Showing 445 changed files with 11,510 additions and 2,292 deletions.
19 changes: 18 additions & 1 deletion generators/python/core_utilities/shared/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ def convert_and_respect_annotation_metadata(
if typing_extensions.is_typeddict(clean_type) and isinstance(object_, typing.Mapping):
return _convert_mapping(object_, clean_type, direction)

if (
typing_extensions.get_origin(clean_type) == typing.Dict
or typing_extensions.get_origin(clean_type) == dict
or clean_type == typing.Dict
) and isinstance(object_, typing.Dict):
key_type = typing_extensions.get_args(clean_type)[0]
value_type = typing_extensions.get_args(clean_type)[1]

return {
key: convert_and_respect_annotation_metadata(
object_=value,
annotation=annotation,
inner_type=value_type,
direction=direction,
)
for key, value in object_.items()
}

# If you're iterating on a string, do not bother to coerce it to a sequence.
if not isinstance(object_, str):
if (
Expand Down Expand Up @@ -101,7 +119,6 @@ def convert_and_respect_annotation_metadata(
)
and isinstance(object_, typing.Sequence)
):

inner_type = typing_extensions.get_args(clean_type)[0]
return [
convert_and_respect_annotation_metadata(
Expand Down
5 changes: 2 additions & 3 deletions generators/python/sdk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ ENV NODE_VERSION=18.20.1
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm ls-remote
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
Expand Down Expand Up @@ -34,8 +33,8 @@ RUN poetry install

ENTRYPOINT ["python", "-m", "src.fern_python.generators.sdk.cli"]

## NOTE: Uncomment the below to generate a flame graph for the python generator.
## To visualize the flamegraph you can run:
## NOTE: Uncomment the below to generate a flame graph for the python generator.
## To visualize the flamegraph you can run:
## - poetry add snakeviz
## - poetry run snakeviz output.prof
# RUN mkdir -p /fern/output
Expand Down
7 changes: 7 additions & 0 deletions generators/python/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# For unreleased changes, use unreleased.yml
- version: 4.2.6
irVersion: 53
changelogEntry:
- type: fix
summary: |
Serialization utilities (necessary when pydantic aliases are removed) now respects dictionaries as well.
- version: 4.2.5
irVersion: 53
changelogEntry:
Expand Down
10 changes: 8 additions & 2 deletions generators/python/src/fern_python/snippet/snippet_writer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from typing import Any, Dict, List, Optional

import fern.ir.resources as ir_types
Expand Down Expand Up @@ -262,7 +261,14 @@ def _get_snippet_for_unknown(
self,
unknown: Any,
) -> AST.Expression:
return AST.Expression(json.dumps(unknown))
if unknown is not None:

def write_unknown(writer: AST.NodeWriter) -> None:
maybe_stringify_unknown = repr(unknown) if type(unknown) is str else unknown
writer.write_line(f"{maybe_stringify_unknown}")

return AST.Expression(AST.CodeWriter(write_unknown))
return AST.Expression("None")

def _get_snippet_for_list_or_set(
self,
Expand Down
Loading

0 comments on commit 539cfc4

Please sign in to comment.