Skip to content

Fix named only arguments #112

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
Feb 14, 2023
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
16 changes: 16 additions & 0 deletions atest/DynamicTypesAnnotationsLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,19 @@ def keyword_optional_with_none(self, arg: Optional[str] = None):
@keyword
def keyword_union_with_none(self, arg: Union[None, Dict, str] = None):
return f"arg: {arg}, type: {type(arg)}"

@keyword
def kw_with_named_arguments(self, *, arg):
print(arg)
return f"arg: {arg}, type: {type(arg)}"

@keyword
def kw_with_many_named_arguments(self, *, arg1, arg2):
print(arg1)
print(arg2)
return f"arg1: {arg1}, type: {type(arg1)}, arg2: {arg2}, type: {type(arg2)}"

@keyword
def kw_with_named_arguments_and_variable_number_args(self, *varargs, arg):
print(arg)
return f"arg: {arg}, type: {type(arg)}"
1 change: 1 addition & 0 deletions atest/Python310Library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from robotlibcore import DynamicCore, keyword


class Python310Library(DynamicCore):

def __init__(self):
Expand Down
24 changes: 12 additions & 12 deletions atest/tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ ${LIBRARY} DynamicLibrary


*** Test Cases ***
Keyword names
Keyword in main
Keyword Names
Keyword In Main
Function
FUNCTION
Method
Custom name
Cust omna me
IF $LIBRARY == "ExtendExistingLibrary" Keyword in extending library
Custom Name
Cust Omna Me
IF $LIBRARY == "ExtendExistingLibrary" Keyword In Extending Library

Method without @keyword are not keyowrds
[Documentation] FAIL GLOB: No keyword with name 'Not keyword' found.*
Expand All @@ -27,27 +27,27 @@ Arguments
'foo', 2, 3 Defaults foo ${2}
'a', 'b', 'c' Defaults a b c

Named arguments
Named Arguments
[Template] Return value should be
'foo', 'bar' Mandatory foo arg2=bar
'1', 2 Mandatory arg2=${2} arg1=1
'x', 'default', 'y' Defaults x arg3=y

Varargs and kwargs
Varargs And Kwargs
[Template] Return value should be
${EMPTY} Varargs and kwargs
'a', 'b', 'c' Varargs and kwargs a b c
a\='1', b\=2 Varargs and kwargs a=1 b=${2}
'a', 'b\=b', c\='c' Varargs and kwargs a b\=b c=c

Embedded arguments
[Documentation] FAIL Work but this fails
Embedded arguments "work"
embeDded ArgumeNtS "Work but this fails"
Embedded Arguments
[Documentation] FAIL Work But This Fails
Embedded Arguments "work"
EmbeDded ArgumeNtS "Work But This Fails"


*** Keywords ***
Return value should be
Return Value Should Be
[Arguments] ${expected} ${keyword} @{args} &{kwargs}
${result} Run Keyword ${keyword} @{args} &{kwargs}
Should Be Equal ${result} ${expected}
5 changes: 4 additions & 1 deletion atest/tests_types.robot
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Keyword Only Arguments Without VarArg
${return} = DynamicTypesAnnotationsLibrary.Keyword Only Arguments No Vararg other=tidii
Should Match ${return} tidii: <class 'str'>

Varargs and KeywordArgs With Typing Hints
Varargs And KeywordArgs With Typing Hints
${return} = DynamicTypesAnnotationsLibrary.Keyword Self And Keyword Only Types
... this_is_mandatory # mandatory argument
... 1 2 3 4 # varargs
Expand Down Expand Up @@ -112,6 +112,9 @@ Python 3.10 New Type Hints
Should Be Equal ${types} arg: {"key": 1}, type: <class 'str'>
END

Keyword With Named Only Arguments
Kw With Named Arguments arg=1


*** Keywords ***
Import DynamicTypesAnnotationsLibrary In Python 3.10 Only
Expand Down
10 changes: 7 additions & 3 deletions src/robotlibcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,22 @@ def _drop_self_from_args(cls, function, arg_spec):
@classmethod
def _get_var_args(cls, arg_spec):
if arg_spec.varargs:
return ["*%s" % arg_spec.varargs]
return [f"*{arg_spec.varargs}"]
return []

@classmethod
def _get_kwargs(cls, arg_spec):
return ["**%s" % arg_spec.varkw] if arg_spec.varkw else []
return [f"**{arg_spec.varkw}"] if arg_spec.varkw else []

@classmethod
def _get_kw_only(cls, arg_spec):
kw_only_args = []
kw_only_defaults = arg_spec.kwonlydefaults if arg_spec.kwonlydefaults else []
for arg in arg_spec.kwonlyargs:
if not arg_spec.kwonlydefaults or arg not in arg_spec.kwonlydefaults:
if not arg_spec.varargs and arg not in kw_only_defaults and not kw_only_args:
kw_only_args.append("*")
kw_only_args.append(arg)
elif arg not in kw_only_defaults:
kw_only_args.append(arg)
else:
value = arg_spec.kwonlydefaults.get(arg, "")
Expand Down
6 changes: 6 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def lint(ctx):
print(f"Lint Robot files {'in ci' if in_ci else ''}")
command = [
"robotidy",
"--transform",
"RenameKeywords",
"--transform",
"RenameTestCases",
"-c",
"RenameTestCases:capitalize_each_word=True",
"--lineseparator",
"unix",
"atest/",
Expand Down
5 changes: 5 additions & 0 deletions utest/test_get_keyword_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,8 @@ def test_keyword_optional_with_none(lib_types):
def test_keyword_union_with_none(lib_types):
types = lib_types.get_keyword_types("keyword_union_with_none")
assert types == {"arg": typing.Union[type(None), typing.Dict, str]}


def test_kw_with_named_arguments(lib_types: DynamicTypesAnnotationsLibrary):
types = lib_types.get_keyword_types("kw_with_named_arguments")
assert types == {}
7 changes: 7 additions & 0 deletions utest/test_robotlibcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ def test_keyword_only_arguments_for_get_keyword_arguments():
assert args("keyword_with_deco_and_signature") == [("arg1", False), ("arg2", False)]


def test_named_only_argumens():
args = DynamicTypesAnnotationsLibrary(1).get_keyword_arguments
assert args("kw_with_named_arguments") == ["*", "arg"]
assert args("kw_with_many_named_arguments") == ["*", "arg1", "arg2"]
assert args("kw_with_named_arguments_and_variable_number_args") == ["*varargs", "arg"]


def test_get_keyword_documentation():
doc = DynamicLibrary().get_keyword_documentation
assert doc("function") == ""
Expand Down