Skip to content

Commit a389b22

Browse files
committed
Unit test for Union problem
1 parent 3d2dcc5 commit a389b22

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/robotlibcore.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@
1818
examples see the project pages at
1919
https://github.com/robotframework/PythonLibCore
2020
"""
21-
2221
import inspect
2322
import os
23+
import typing
2424

25+
from robot import version as robot_version
2526
from robot.utils import PY_VERSION
2627

27-
try:
28-
import typing
29-
except ImportError:
30-
typing = None
3128

3229
from robot.api.deco import keyword # noqa F401
3330

31+
RF32 = robot_version < '4.'
3432

3533
__version__ = '2.2.2.dev1'
3634

@@ -51,6 +49,8 @@ def add_library_components(self, library_components):
5149
if callable(func) and hasattr(func, 'robot_name'):
5250
kw = getattr(component, name)
5351
kw_name = func.robot_name or name
52+
if kw_name == "keyword_optional_with_none":
53+
print(kw_name)
5454
self.keywords[kw_name] = kw
5555
self.keywords_spec[kw_name] = KeywordBuilder.build(kw)
5656
# Expose keywords as attributes both using original
@@ -246,8 +246,10 @@ def _get_typing_hints(cls, function):
246246
# remove return and self statements
247247
if arg_with_hint not in all_args:
248248
hints.pop(arg_with_hint)
249-
default = cls._get_defaults(arg_spec)
250-
return cls._remove_optional_none_type_hints(hints, default)
249+
if RF32:
250+
default = cls._get_defaults(arg_spec)
251+
return cls._remove_optional_none_type_hints(hints, default)
252+
return hints
251253

252254
@classmethod
253255
def _args_as_list(cls, function, arg_spec):

utest/test_get_keyword_types.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
from typing import List, Union
1+
import sys
2+
from os.path import dirname, abspath, join
23

34
import pytest
5+
import typing
6+
7+
from robotlibcore import RF32
8+
9+
from typing import List, Union
410

11+
curdir = dirname(abspath(__file__))
12+
atest_dir = join(curdir, '..', 'atest')
13+
src = join(curdir, '..', 'src')
14+
sys.path.insert(0, src)
15+
sys.path.insert(0, atest_dir)
516
from DynamicTypesAnnotationsLibrary import DynamicTypesAnnotationsLibrary
617
from DynamicTypesAnnotationsLibrary import CustomObject
718
from DynamicTypesLibrary import DynamicTypesLibrary
@@ -182,6 +193,15 @@ def test_keyword_with_decorator_arguments(lib_types):
182193
assert types == {'arg1': bool, 'arg2': bool}
183194

184195

185-
def test_keyword_optional_with_none_1(lib_types):
186-
types = lib_types.get_keyword_types('keyword_optional_with_none')
187-
assert types == {'arg': Union[str, type(None)]}
196+
@pytest.mark.skipif(RF32, reason='Only for RF4+')
197+
def test_keyword_optional_with_none_rf32(lib_types):
198+
lib = DynamicTypesAnnotationsLibrary("111")
199+
types = lib.get_keyword_types('keyword_optional_with_none')
200+
assert types == {'arg': typing.Union[str, type(None)]}
201+
202+
203+
@pytest.mark.skipif(not RF32, reason='Only for RF3.2+')
204+
def test_keyword_optional_with_none_rf32(lib_types):
205+
lib = DynamicTypesAnnotationsLibrary("111")
206+
types = lib.get_keyword_types('keyword_optional_with_none')
207+
assert types == {'arg': str}

0 commit comments

Comments
 (0)