Skip to content

Commit

Permalink
Fix writing optional Nullable values (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt authored Jul 22, 2024
1 parent 4d77f1d commit a61c4b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions matter_server/common/helpers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
get_type_hints,
)

from chip.clusters.Types import Nullable
from chip.clusters.Types import Nullable, NullValue
from chip.tlv import float32, uint

if TYPE_CHECKING:
Expand Down Expand Up @@ -168,11 +168,14 @@ def parse_value(
}
# handle Union type
if origin is Union or origin is UnionType:
# try all possible types
sub_value_types = get_args(value_type)
# return early if value is None and None or Nullable allowed
if value is None and Nullable in sub_value_types and allow_sdk_types:
return NullValue
if value is None and NoneType in sub_value_types:
return None
# try all possible types
for sub_arg_type in sub_value_types:
if value is NoneType and sub_arg_type is NoneType:
return value
# try them all until one succeeds
try:
return parse_value(
Expand Down
10 changes: 10 additions & 0 deletions tests/common/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ def test_parse_value():
)
== NullValue
)
assert (
parse_value(
"test",
None,
Union[None, int, Nullable],
allow_none=False,
allow_sdk_types=True,
)
== NullValue
)

0 comments on commit a61c4b0

Please sign in to comment.