Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changelog.d/12.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix parsing Python 3.10+ union type hints when evaluating model refs
1 change: 1 addition & 0 deletions changelog.d/14.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix parsing Python 3.10+ union type hints when encoding search filters, and decoding record values
9 changes: 4 additions & 5 deletions openstack_odooclient/base/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,9 @@ def _getattr_model_ref(
attr_type_origin = get_type_origin(attr_type)
if attr_type_origin is Union or attr_type_origin is UnionType:
unsupported_union = (
"Only unions of the format Optional[T], "
"Union[T, type(None)] or Union[T, Literal[False]] "
"are supported for singular model refs, "
f"found type hint: {attr_type}"
"Only unions of the format 'T | None' "
"or 'T | Literal[False]' are supported for singular "
f"model refs, found type hint: {attr_type}"
)
union_types = set(get_type_args(attr_type))
if len(union_types) > 2: # noqa: PLR2004
Expand Down Expand Up @@ -465,7 +464,7 @@ def _decode_value(cls, type_hint: Any, value: Any) -> Any:
# Not suitable for handling complicated union structures.
# TODO(callumdickinson): Find a way to handle complicated
# union structures more smartly.
if value_type is Union:
if value_type is Union or value_type is UnionType:
attr_union_types = get_type_args(type_hint)
if len(attr_union_types) == 2: # noqa: PLR2004
# T | None
Expand Down
19 changes: 13 additions & 6 deletions openstack_odooclient/base/record_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import builtins

from datetime import date, datetime
from types import MappingProxyType
from types import MappingProxyType, UnionType
from typing import (
TYPE_CHECKING,
Annotated,
Expand Down Expand Up @@ -961,25 +961,32 @@ def _encode_value(self, type_hint: Any, value: Any) -> Any:
return value
# For every other field type, parse the possible value types
# from the type hint.
# First, remove Annotated to get the actual attribute type.
type_hint_origin = get_type_origin(type_hint) or type_hint
attr_type = (
get_type_args(type_hint)[0]
if type_hint_origin is Annotated
else type_hint_origin
else type_hint
)
# Next, get a list of allowed values types from the attribute type.
# If there is only one, create a list containing only that type.
attr_type_origin = get_type_origin(attr_type) or attr_type
value_types = (
get_type_args(attr_type)
if attr_type_origin is Union
else [attr_type_origin]
if attr_type_origin is Union or attr_type_origin is UnionType
else [attr_type]
)
# Recursively handle the types that need to be serialised.
for value_type in value_types:
value_type_origin = get_type_origin(value_type) or value_type
if value_type is date and isinstance(value, date):
return value.strftime(DEFAULT_SERVER_DATE_FORMAT)
if value_type is datetime and isinstance(value, datetime):
return value.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
if value_type is list and isinstance(value, (list, set, tuple)):
v_type = get_type_args(type_hint)[0]
if value_type_origin is list and isinstance(
value,
(list, set, tuple),
):
v_type = get_type_args(value_type)[0]
return [self._encode_value(v_type, v) for v in value]
return value