Skip to content

Commit 9dd3bbf

Browse files
committed
fix test failing on py 1.13. Merge two unit test files per feedback
Signed-off-by: Filinto Duran <[email protected]>
1 parent 924541c commit 9dd3bbf

File tree

3 files changed

+428
-443
lines changed

3 files changed

+428
-443
lines changed

dapr/clients/grpc/_conversation_helpers.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import string
1919
from dataclasses import fields, is_dataclass
2020
from enum import Enum
21+
from types import UnionType
2122
from typing import (
2223
Any,
2324
Callable,
@@ -854,20 +855,26 @@ def _coerce_literal(value: Any, lit_args: List[Any]) -> Any:
854855
raise ValueError(f'{value!r} not in allowed literals {lit_args!r}')
855856

856857

858+
def _is_union(t) -> bool:
859+
origin = get_origin(t)
860+
return origin in (Union, UnionType) # handles 3.8–3.13+
861+
862+
857863
def _coerce_and_validate(value: Any, expected_type: Any) -> Any:
858-
origin = get_origin(expected_type)
859864
args = get_args(expected_type)
860865

861866
if expected_type is Any:
862867
raise TypeError('We cannot handle parameters with type Any')
863868

864869
# Optional[T] -> Union[T, None]
865-
if origin is Union:
870+
if _is_union(expected_type):
866871
# try each option
867872
last_err: Optional[Exception] = None
868873
for opt in args:
869-
if opt is type(None) and value is None:
870-
return None
874+
if opt is type(None):
875+
if value is None:
876+
return None
877+
continue
871878
try:
872879
return _coerce_and_validate(value, opt)
873880
except Exception as e:
@@ -877,6 +884,8 @@ def _coerce_and_validate(value: Any, expected_type: Any) -> Any:
877884
str(last_err) if last_err else f'Cannot coerce {value!r} to {expected_type}'
878885
)
879886

887+
origin = get_origin(expected_type)
888+
880889
# Literal
881890
if origin is Literal:
882891
return _coerce_literal(value, list(args))

0 commit comments

Comments
 (0)