Skip to content

Commit 34f1999

Browse files
author
Callum Dickinson
committed
Fix more Python 3.10+ union type parsing issues
* Fix parsing Python 3.10+ type hints when encoding search filters. * Fix parsing Python 3.10+ type hints when decoding record values. * Add missing release note for fixing parsing Python 3.10+ type hints when evaluating model refs.
1 parent 33e5d51 commit 34f1999

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

changelog.d/12.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix parsing Python 3.10+ union type hints when evaluating model refs

changelog.d/14.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix parsing Python 3.10+ union type hints when encoding search filters, and decoding record values

openstack_odooclient/base/record.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,9 @@ def _getattr_model_ref(
395395
attr_type_origin = get_type_origin(attr_type)
396396
if attr_type_origin is Union or attr_type_origin is UnionType:
397397
unsupported_union = (
398-
"Only unions of the format Optional[T], "
399-
"Union[T, type(None)] or Union[T, Literal[False]] "
400-
"are supported for singular model refs, "
401-
f"found type hint: {attr_type}"
398+
"Only unions of the format 'T | None' "
399+
"or 'T | Literal[False]' are supported for singular "
400+
f"model refs, found type hint: {attr_type}"
402401
)
403402
union_types = set(get_type_args(attr_type))
404403
if len(union_types) > 2: # noqa: PLR2004
@@ -465,7 +464,7 @@ def _decode_value(cls, type_hint: Any, value: Any) -> Any:
465464
# Not suitable for handling complicated union structures.
466465
# TODO(callumdickinson): Find a way to handle complicated
467466
# union structures more smartly.
468-
if value_type is Union:
467+
if value_type is Union or value_type is UnionType:
469468
attr_union_types = get_type_args(type_hint)
470469
if len(attr_union_types) == 2: # noqa: PLR2004
471470
# T | None

openstack_odooclient/base/record_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import builtins
1919

2020
from datetime import date, datetime
21-
from types import MappingProxyType
21+
from types import MappingProxyType, UnionType
2222
from typing import (
2323
TYPE_CHECKING,
2424
Annotated,
@@ -970,7 +970,7 @@ def _encode_value(self, type_hint: Any, value: Any) -> Any:
970970
attr_type_origin = get_type_origin(attr_type) or attr_type
971971
value_types = (
972972
get_type_args(attr_type)
973-
if attr_type_origin is Union
973+
if attr_type_origin is Union or attr_type_origin is UnionType
974974
else [attr_type_origin]
975975
)
976976
# Recursively handle the types that need to be serialised.

0 commit comments

Comments
 (0)