Skip to content

Commit

Permalink
fix(python): lists of objects with literals now respect the custom ex…
Browse files Browse the repository at this point in the history
…clude unset logic (#4752)
  • Loading branch information
armandobelardo committed Sep 26, 2024
1 parent b36fd9a commit d95bbc6
Show file tree
Hide file tree
Showing 348 changed files with 9,652 additions and 1,367 deletions.
5 changes: 5 additions & 0 deletions fern/pages/changelogs/fastapi/2024-09-26.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.5.0-rc5
**`(fix):`** Pydantic utilities now correctly handles cases where you have a Pydantic model, with a list of pydantic models as a field, where those models have literals.
Effectively, `deep_union_pydantic_objects` now handles lists of objects and merges them appropriately.


5 changes: 5 additions & 0 deletions fern/pages/changelogs/pydantic/2024-09-26.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.4.7-rc1
**`(fix):`** Pydantic utilities now correctly handles cases where you have a Pydantic model, with a list of pydantic models as a field, where those models have literals.
Effectively, `deep_union_pydantic_objects` now handles lists of objects and merges them appropriately.


5 changes: 5 additions & 0 deletions fern/pages/changelogs/python-sdk/2024-09-26.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 4.2.7-rc2
**`(fix):`** Pydantic utilities now correctly handles cases where you have a Pydantic model, with a list of pydantic models as a field, where those models have literals.
Effectively, `deep_union_pydantic_objects` now handles lists of objects and merges them appropriately.


22 changes: 21 additions & 1 deletion generators/python/core_utilities/shared/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,33 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
return convert_and_respect_annotation_metadata(object_=dict_dump, annotation=self.__class__, direction="write")


def _union_list_of_pydantic_dicts(
source: typing.List[typing.Any], destination: typing.List[typing.Any]
) -> typing.List[typing.Any]:
converted_list: typing.List[typing.Any] = []
for i, item in enumerate(source):
destination_value = destination[i] # type: ignore
if isinstance(item, dict):
converted_list.append(deep_union_pydantic_dicts(item, destination_value))
elif isinstance(item, list):
converted_list.append(_union_list_of_pydantic_dicts(item, destination_value))
else:
converted_list.append(item)
return converted_list


def deep_union_pydantic_dicts(
source: typing.Dict[str, typing.Any], destination: typing.Dict[str, typing.Any]
) -> typing.Dict[str, typing.Any]:
for key, value in source.items():
node = destination.setdefault(key, {})
if isinstance(value, dict):
node = destination.setdefault(key, {})
deep_union_pydantic_dicts(value, node)
# Note: we do not do this same processing for sets given we do not have sets of models
# and given the sets are unordered, the processing of the set and matching objects would
# be non-trivial.
elif isinstance(value, list):
destination[key] = _union_list_of_pydantic_dicts(value, node)
else:
destination[key] = value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,33 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
return super().dict(**kwargs_with_defaults_exclude_unset_include_fields)


def _union_list_of_pydantic_dicts(
source: typing.List[typing.Any], destination: typing.List[typing.Any]
) -> typing.List[typing.Any]:
converted_list: typing.List[typing.Any] = []
for i, item in enumerate(source):
destination_value = destination[i] # type: ignore
if isinstance(item, dict):
converted_list.append(deep_union_pydantic_dicts(item, destination_value))
elif isinstance(item, list):
converted_list.append(_union_list_of_pydantic_dicts(item, destination_value))
else:
converted_list.append(item)
return converted_list


def deep_union_pydantic_dicts(
source: typing.Dict[str, typing.Any], destination: typing.Dict[str, typing.Any]
) -> typing.Dict[str, typing.Any]:
for key, value in source.items():
node = destination.setdefault(key, {})
if isinstance(value, dict):
node = destination.setdefault(key, {})
deep_union_pydantic_dicts(value, node)
# Note: we do not do this same processing for sets given we do not have sets of models
# and given the sets are unordered, the processing of the set and matching objects would
# be non-trivial.
elif isinstance(value, list):
destination[key] = _union_list_of_pydantic_dicts(value, node)
else:
destination[key] = value

Expand Down
10 changes: 9 additions & 1 deletion generators/python/fastapi/versions.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# For unreleased changes, use unreleased.yml
# For unreleased changes, use unreleased.yml
- version: 1.5.0-rc5
irVersion: 53
changelogEntry:
- type: fix
summary: |
Pydantic utilities now correctly handles cases where you have a Pydantic model, with a list of pydantic models as a field, where those models have literals.
Effectively, `deep_union_pydantic_objects` now handles lists of objects and merges them appropriately.
- version: 1.5.0-rc4
irVersion: 53
changelogEntry:
Expand Down
8 changes: 8 additions & 0 deletions generators/python/pydantic/versions.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# For unreleased changes, use unreleased.yml
- version: 1.4.7-rc1
irVersion: 53
changelogEntry:
- type: fix
summary: |
Pydantic utilities now correctly handles cases where you have a Pydantic model, with a list of pydantic models as a field, where those models have literals.
Effectively, `deep_union_pydantic_objects` now handles lists of objects and merges them appropriately.
- version: 1.4.7-rc0
irVersion: 53
changelogEntry:
Expand Down
8 changes: 8 additions & 0 deletions generators/python/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# For unreleased changes, use unreleased.yml
- version: 4.2.7-rc2
irVersion: 53
changelogEntry:
- type: fix
summary: |
Pydantic utilities now correctly handles cases where you have a Pydantic model, with a list of pydantic models as a field, where those models have literals.
Effectively, `deep_union_pydantic_objects` now handles lists of objects and merges them appropriately.
- version: 4.2.7-rc1
irVersion: 53
changelogEntry:
Expand Down
Loading

0 comments on commit d95bbc6

Please sign in to comment.