Skip to content

Commit

Permalink
Argument conversion: Accept any mapping matching TypedDict as-is
Browse files Browse the repository at this point in the history
The initial fix for #5114 required arguments to be dicts, this change
allows any mapping.
  • Loading branch information
pekkaklarck committed Apr 22, 2024
1 parent f85d439 commit c8518d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion atest/testdata/keywords/type_conversion/unions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date, timedelta
from collections.abc import Mapping
from numbers import Rational
from typing import List, Optional, TypedDict, Union

Expand Down Expand Up @@ -64,7 +65,10 @@ def union_with_typeddict(argument: Union[XD, None], expected):
assert argument == eval(expected), '%r != %s' % (argument, expected)


def union_with_str_and_typeddict(argument: Union[str, XD], expected):
def union_with_str_and_typeddict(argument: Union[str, XD], expected, non_dict_mapping=False):
if non_dict_mapping:
assert isinstance(argument, Mapping) and not isinstance(argument, dict)
argument = dict(argument)
assert argument == eval(expected), '%r != %s' % (argument, expected)


Expand Down
2 changes: 2 additions & 0 deletions atest/testdata/keywords/type_conversion/unions.robot
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Union with str and TypedDict
[Template] Union with str and TypedDict
{'x': 1} "{'x': 1}"
${{{'x': 1}}} {'x': 1}
${{type('NonDictMapping', (collections.abc.Mapping,), {'__getitem__': lambda s, k: {'x': 1}[k], '__iter__': lambda s: iter({'x': 1}), '__len__': lambda s: 1})()}}
... {'x': 1} non_dict_mapping=True
${{{'bad': 1}}} "{'bad': 1}"
${{{'x': '1'}}} "{'x': '1'}"

Expand Down
2 changes: 1 addition & 1 deletion src/robot/running/arguments/typeconverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def handles(cls, type_info: 'TypeInfo') -> bool:
return type_info.is_typed_dict

def no_conversion_needed(self, value):
if not isinstance(value, dict):
if not isinstance(value, Mapping):
return False
for key in value:
try:
Expand Down

0 comments on commit c8518d2

Please sign in to comment.