Skip to content

Fix library embedded argument handling #5425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
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
38 changes: 26 additions & 12 deletions atest/robot/variables/variable_types.robot
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ Variable section: With invalid values or types
Variable section: Invalid syntax
Error In File
... 3 variables/variable_types.robot
... 17 Setting variable '\${BAD_TYPE: hahaa}' failed: Unrecognized type 'hahaa'.
... 18 Setting variable '\${BAD_TYPE: hahaa}' failed: Unrecognized type 'hahaa'.
Error In File
... 4 variables/variable_types.robot 19
... 4 variables/variable_types.robot 20
... Setting variable '\@{BAD_LIST_TYPE: xxxxx}' failed: Unrecognized type 'xxxxx'.
Error In File
... 5 variables/variable_types.robot 21
... 5 variables/variable_types.robot 22
... Setting variable '\&{BAD_DICT_TYPE: aa=bb}' failed: Unrecognized type 'aa'.
Error In File
... 6 variables/variable_types.robot 22
... 6 variables/variable_types.robot 23
... Setting variable '\&{INVALID_DICT_TYPE1: int=list[int}' failed:
... Parsing type 'dict[int, list[int]' failed:
... Error at end: Closing ']' missing.
... pattern=False
Error In File
... 7 variables/variable_types.robot 23
... 7 variables/variable_types.robot 24
... Setting variable '\&{INVALID_DICT_TYPE2: int=listint]}' failed:
... Parsing type 'dict[int, listint]]' failed:
... Error at index 18: Extra content after 'dict[int, listint]'.
... pattern=False
Error In File
... 8 variables/variable_types.robot 20
... 9 variables/variable_types.robot 21
... Setting variable '\&{BAD_DICT_VALUE: str=int}' failed:
... Value '{'x': 'a', 'y': 'b'}' (DotDict) cannot be converted to dict[str, int]:
... Item 'x' got value 'a' that cannot be converted to integer.
... pattern=False
Error In File
... 9 variables/variable_types.robot 18
... 10 variables/variable_types.robot 19
... Setting variable '\@{BAD_LIST_VALUE: int}' failed:
... Value '['1', 'hahaa']' (list) cannot be converted to list[int]:
... Item '1' got value 'hahaa' that cannot be converted to integer.
... pattern=False
Error In File
... 10 variables/variable_types.robot 16
... 11 variables/variable_types.robot 17
... Setting variable '\${BAD_VALUE: int}' failed: Value 'not int' cannot be converted to integer.
... pattern=False

Expand All @@ -75,7 +75,7 @@ VAR syntax: Type can not be set as variable
VAR syntax: Type syntax is not resolved from variable
Check Test Case ${TESTNAME}

Vvariable assignment
Variable assignment
Check Test Case ${TESTNAME}

Variable assignment: List
Expand All @@ -99,6 +99,9 @@ Variable assignment: Invalid type for list
Variable assignment: Invalid variable type for dictionary
Check Test Case ${TESTNAME}

Variable assignment: No type when using variable
Check Test Case ${TESTNAME}

Variable assignment: Multiple
Check Test Case ${TESTNAME}

Expand Down Expand Up @@ -136,15 +139,15 @@ User keyword: Invalid value
User keyword: Invalid type
Check Test Case ${TESTNAME}
Error In File
... 0 variables/variable_types.robot 333
... 0 variables/variable_types.robot 355
... Creating keyword 'Bad type' failed:
... Invalid argument specification: Invalid argument '\${arg: bad}':
... Unrecognized type 'bad'.

User keyword: Invalid assignment with kwargs k_type=v_type declaration
Check Test Case ${TESTNAME}
Error In File
... 1 variables/variable_types.robot 337
... 1 variables/variable_types.robot 359
... Creating keyword 'Kwargs does not support key=value type syntax' failed:
... Invalid argument specification: Invalid argument '\&{kwargs: int=float}':
... Unrecognized type 'int=float'.
Expand All @@ -155,6 +158,17 @@ Embedded arguments
Embedded arguments: With variables
Check Test Case ${TESTNAME}

Embedded arguments: Invalid type in library
Check Test Case ${TESTNAME}
Error in library
... Embedded
... Adding keyword 'bad_type' failed:
... Invalid embedded argument '\${value: bad}': Unrecognized type 'bad'.
... index=8

Embedded arguments: Type only in embedded
Check Test Case ${TESTNAME}

Embedded arguments: Invalid value
Check Test Case ${TESTNAME}

Expand All @@ -164,7 +178,7 @@ Embedded arguments: Invalid value from variable
Embedded arguments: Invalid type
Check Test Case ${TESTNAME}
Error In File
... 2 variables/variable_types.robot 357
... 2 variables/variable_types.robot 379
... Creating keyword 'Embedded invalid type \${x: invalid}' failed:
... Invalid embedded argument '\${x: invalid}':
... Unrecognized type 'invalid'.
Expand Down
8 changes: 8 additions & 0 deletions atest/testdata/test_libraries/Embedded.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ def called_times(self, times):
raise AssertionError(
f"Called {self.called} time(s), expected {times} time(s)."
)

@keyword("Embedded invalid type in library ${value: bad}")
def bad_type(self, value: str):
return value

@keyword("Type only in embedded ${value: int}")
def no_type(self, value):
return value
26 changes: 24 additions & 2 deletions atest/testdata/variables/variable_types.robot
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*** Settings ***
Library ../test_libraries/Embedded.py
Variables extended_variables.py


Expand Down Expand Up @@ -75,6 +76,9 @@ VAR syntax
Should be equal ${x} 123 type=int
VAR ${x: int} 1 2 3 separator=
Should be equal ${x} 123 type=int
VAR ${name} x
VAR ${${name}: int} 432
Should be equal ${x} 432 type=int

VAR syntax: List
VAR ${x: list} [1, "2", 3]
Expand All @@ -89,6 +93,8 @@ VAR syntax: Dictionary
Should be equal ${x} {"1": 2, "3": 4} type=dict
VAR &{x: int=str} 3=4 5=6
Should be equal ${x} {3: "4", 5: "6"} type=dict
VAR &{x: int = str} 100=200 300=400
Should be equal ${x} {100: "200", 300: "400"} type=dict
VAR &{x: int=dict[str, float]} 30={"key": 1} 40={"key": 2.3}
Should be equal ${x} {30: {"key": 1.0}, 40: {"key": 2.3}} type=dict

Expand All @@ -115,7 +121,7 @@ VAR syntax: Type syntax is not resolved from variable
VAR ${${type}} 4242
Should be equal ${tidii: int} 4242 type=str

Vvariable assignment
Variable assignment
${x: int} = Set Variable 42
Should be equal ${x} 42 type=int

Expand Down Expand Up @@ -162,6 +168,13 @@ Variable assignment: Invalid variable type for dictionary
[Documentation] FAIL Unrecognized type 'int=str'.
${x: int=str} = Create dictionary 1=2 3=4

Variable assignment: No type when using variable
[Documentation] FAIL
... Resolving variable '\${x: str}' failed: SyntaxError: invalid syntax (<string>, line 1)
${x: date} Set Variable 2025-04-30
Should be equal ${x} 2025-04-30 type=date
Should be equal ${x: str} 2025-04-30 type=str

Variable assignment: Multiple
${a: int} ${b: float} = Create List 1 2.3
Should be equal ${a} 1 type=int
Expand Down Expand Up @@ -258,7 +271,6 @@ User keyword: Invalid assignment with kwargs k_type=v_type declaration
Kwargs does not support key=value type syntax

Embedded arguments
[Tags] kala
Embedded 1 and 2
Embedded type 1 and no type 2
Embedded type with custom regular expression 111
Expand All @@ -268,6 +280,16 @@ Embedded arguments: With variables
VAR ${y} ${2.0}
Embedded ${x} and ${y}

Embedded arguments: Invalid type in library
[Documentation] FAIL No keyword with name 'Embedded Invalid type in library 111' found.
Embedded Invalid type in library 111

Embedded arguments: Type only in embedded
[Documentation] FAIL
... Embedded arguments do not support type information with library keywords: \
... 'Embedded.Type only in embedded \${value: int}'. Use normal type hints instead.
Type only in embedded 987

Embedded arguments: Invalid value
[Documentation] FAIL ValueError: Argument 'kala' cannot be converted to integer.
Embedded 1 and kala
Expand Down
6 changes: 6 additions & 0 deletions src/robot/running/librarykeywordrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ class EmbeddedArgumentsRunner(LibraryKeywordRunner):

def __init__(self, keyword: "LibraryKeyword", name: "str"):
super().__init__(keyword, name)
if any(keyword.embedded.types):
raise DataError(
"Embedded arguments do not support type information "
f"with library keywords: '{keyword.full_name}'. "
"Use normal type hints instead."
)
self.embedded_args = keyword.embedded.parse_args(name)

def _resolve_arguments(
Expand Down