Skip to content

Commit 99030f5

Browse files
committed
Fixed get_keyword_types if seld has typing hints
1 parent c3e0d2d commit 99030f5

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

atest/DynamicTypesAnnotationsLibrary.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ def keyword_define_return_type(self, arg: str) -> None:
4343
return None
4444

4545
@keyword
46-
def keyword_forward_references(self, arg: 'CustomObject'):
46+
def keyword_forward_references(self: 'DynamicTypesAnnotationsLibrary', arg: 'CustomObject'):
4747
return arg
4848

4949
@keyword
50-
def keyword_with_annotations_and_default(self, arg: str = 'Foobar'):
50+
def keyword_with_annotations_and_default(self: 'DynamicTypesAnnotationsLibrary', arg: str = 'Foobar'):
5151
return arg
5252

5353
@keyword
5454
def keyword_with_webdriver(self, arg: CustomObject):
5555
return arg
5656

5757
@keyword
58-
def keyword_default_and_annotation(self, arg1: int, arg2=False) -> str:
58+
def keyword_default_and_annotation(self: 'DynamicTypesAnnotationsLibrary', arg1: int, arg2=False) -> str:
5959
return '%s: %s, %s: %s' % (arg1, type(arg1), arg2, type(arg2))
6060

6161
@keyword(types={'arg': str})
62-
def keyword_robot_types_and_annotations(self, arg: int):
62+
def keyword_robot_types_and_annotations(self: 'DynamicTypesAnnotationsLibrary', arg: int):
6363
return '%s: %s' % (arg, type(arg))
6464

6565
@keyword(types=None)
@@ -71,7 +71,7 @@ def keyword_robot_types_and_bool_defaults(self, arg1, arg2=False):
7171
return '%s: %s, %s: %s' % (arg1, type(arg1), arg2, type(arg2))
7272

7373
@keyword
74-
def keyword_exception_annotations(self, arg: 'NotHere'):
74+
def keyword_exception_annotations(self: 'DynamicTypesAnnotationsLibrary', arg: 'NotHere'):
7575
return arg
7676

7777
@keyword
@@ -87,7 +87,7 @@ def keyword_only_arguments_no_vararg(self, *, other):
8787
return f'{other}: {type(other)}'
8888

8989
@keyword
90-
def keyword_only_arguments_many_positional_and_default(self, *varargs, one, two, three, four=True, five=None, six=False):
90+
def keyword_only_arguments_many_positional_and_default(self: 'DynamicTypesAnnotationsLibrary', *varargs, one, two, three, four=True, five=None, six=False):
9191
return f'{varargs}, {one}, {two}, {three}, {four}, {five}, {six}'
9292

9393
@keyword
@@ -103,5 +103,13 @@ def keyword_mandatory_and_keyword_only_arguments(self, arg: int, *vararg, some=T
103103
return f'{arg}, {vararg}, {some}'
104104

105105
@keyword
106-
def keyword_all_args(self, mandatory, positional=1, *varargs, other, value=False, **kwargs):
106+
def keyword_all_args(self: 'DynamicTypesAnnotationsLibrary', mandatory, positional=1, *varargs, other, value=False, **kwargs):
107+
return True
108+
109+
@keyword
110+
def keyword_self_and_types(self: 'DynamicTypesAnnotationsLibrary', mandatory: str, *varargs, other: bool, **kwargs):
111+
return True
112+
113+
@keyword
114+
def keyword_self_and_keyword_only_types(self: 'DynamicTypesAnnotationsLibrary', mandatory, *varargs, other: bool, **kwargs):
107115
return True

src/robotlibcore.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ def __get_typing_hints(self, method):
145145
except Exception:
146146
hints = method.__annotations__
147147
hints.pop('return', None)
148+
spec = ArgumentSpec.from_function(method)
149+
for arg in hints.copy():
150+
# Drop self
151+
if arg not in spec.positional and arg not in spec.kwonlyargs:
152+
hints.pop(arg)
148153
return hints
149154

150155
def __join_defaults_with_types(self, method, types):

utest/test_get_keyword_types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,20 @@ def test_keyword_only_arguments_many(lib_types):
193193
def test_keyword_only_arguments_many(lib_types):
194194
types = lib_types.get_keyword_types('keyword_only_arguments_many_positional_and_default')
195195
assert types == {'four': bool, 'five': type(None), 'six': bool}
196+
197+
198+
@pytest.mark.skipif(PY2, reason='Only applicable on Python 3')
199+
def test_keyword_all_args(lib_types):
200+
types = lib_types.get_keyword_types('keyword_all_args')
201+
assert types == {'value': bool}
202+
203+
204+
@pytest.mark.skipif(PY2, reason='Only applicable on Python 3')
205+
def test_keyword_self_and_types(lib_types):
206+
types = lib_types.get_keyword_types('keyword_self_and_types')
207+
assert types == {'mandatory': str, 'other': bool}
208+
209+
@pytest.mark.skipif(PY2, reason='Only applicable on Python 3')
210+
def test_keyword_self_and_keyword_only_types(lib_types):
211+
types = lib_types.get_keyword_types('keyword_self_and_keyword_only_types')
212+
assert types == {'other': bool}

0 commit comments

Comments
 (0)