RobotFramework: Getting "TypeError: Subscripted generics cannot be used with class and instance checks"
RobotFramework: Getting "TypeError: Subscripted generics cannot be used with class and instance checks"
仅当通过 RobotFramework 尝试使用 isinstance()
函数时,我收到“类型错误:下标泛型不能与 class 和实例检查一起使用”错误。
请让我知道我是否做错了什么。
创建了以下目录结构,
|
|- lib/
| |- mylib.py
|- tests/
|- mytest.robot
这里是mylib.py
的内容
from typing import Union, List
class mylib:
@staticmethod
def fun(x: Union[str, List[str]]):
if isinstance(x, str):
x = [x]
print(x)
mytest.robot
的内容
*** Settings ***
Library ../lib/mylib.py
*** Test Cases ***
My Test
${y}= Create List abc xyz
mylib.fun x=${y}
这是 运行 输出,
D:\tmp\robot>python -m robot --loglevel DEBUG tests
==============================================================================
Tests
==============================================================================
Tests.Mytest
==============================================================================
My Test | FAIL |
TypeError: Subscripted generics cannot be used with class and instance checks
------------------------------------------------------------------------------
Tests.Mytest | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Tests | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Output: D:\tmp\robot\output.xml
Log: D:\tmp\robot\log.html
Report: D:\tmp\robot\report.html
log.html 显示以下错误,
如果我 运行 通过 Python 解释器(使用 Python 3.8.5)使用相同的代码,它就可以工作!
>>> from typing import Union, List
>>>
>>> class mylib:
... @staticmethod
... def fun(x: Union[str, List[str]]):
... if isinstance(x, str):
... x = [x]
... print(x)
...
>>> mylib.fun('abc')
['abc']
>>> mylib.fun(['abc', 'xyz'])
['abc', 'xyz']
>>>
使用以下 RobotFramework 版本。
C:\Users\amit_tendulkar>python -m robot --version
Robot Framework 4.0.1 (Python 3.8.5 on win32)
RobotFramework 中可能出了什么问题?
更新: 如果我将函数定义更改为以下(List
而不是 List[str]
),测试开始通过,
class mylib:
@staticmethod
def fun(x: Union[str, List]):
if isinstance(x, str):
x = [x]
print(x)
看起来问题出在旧版本的 RobotFramework (4.0.1) 上。
我将我的 RobotFramework 升级到 4.2,上面的测试用例开始工作没有任何问题。
PS D:\personal\robot_experiment> py -m robot --version
Robot Framework 4.1.2 (Python 3.8.10 on win32)
仅当通过 RobotFramework 尝试使用 isinstance()
函数时,我收到“类型错误:下标泛型不能与 class 和实例检查一起使用”错误。
请让我知道我是否做错了什么。
创建了以下目录结构,
|
|- lib/
| |- mylib.py
|- tests/
|- mytest.robot
这里是mylib.py
from typing import Union, List
class mylib:
@staticmethod
def fun(x: Union[str, List[str]]):
if isinstance(x, str):
x = [x]
print(x)
mytest.robot
*** Settings ***
Library ../lib/mylib.py
*** Test Cases ***
My Test
${y}= Create List abc xyz
mylib.fun x=${y}
这是 运行 输出,
D:\tmp\robot>python -m robot --loglevel DEBUG tests
==============================================================================
Tests
==============================================================================
Tests.Mytest
==============================================================================
My Test | FAIL |
TypeError: Subscripted generics cannot be used with class and instance checks
------------------------------------------------------------------------------
Tests.Mytest | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Tests | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Output: D:\tmp\robot\output.xml
Log: D:\tmp\robot\log.html
Report: D:\tmp\robot\report.html
log.html 显示以下错误,
如果我 运行 通过 Python 解释器(使用 Python 3.8.5)使用相同的代码,它就可以工作!
>>> from typing import Union, List
>>>
>>> class mylib:
... @staticmethod
... def fun(x: Union[str, List[str]]):
... if isinstance(x, str):
... x = [x]
... print(x)
...
>>> mylib.fun('abc')
['abc']
>>> mylib.fun(['abc', 'xyz'])
['abc', 'xyz']
>>>
使用以下 RobotFramework 版本。
C:\Users\amit_tendulkar>python -m robot --version
Robot Framework 4.0.1 (Python 3.8.5 on win32)
RobotFramework 中可能出了什么问题?
更新: 如果我将函数定义更改为以下(List
而不是 List[str]
),测试开始通过,
class mylib:
@staticmethod
def fun(x: Union[str, List]):
if isinstance(x, str):
x = [x]
print(x)
看起来问题出在旧版本的 RobotFramework (4.0.1) 上。
我将我的 RobotFramework 升级到 4.2,上面的测试用例开始工作没有任何问题。
PS D:\personal\robot_experiment> py -m robot --version
Robot Framework 4.1.2 (Python 3.8.10 on win32)