Pytest crashes when tested method has parameters defined with hints like Optional, Union.
Example:
from typing import Optional
def method(src: Optional(str)):
return src
def test_optional():
assert 'test' == method('test')
Result:
================================================================================ test session starts ================================================================================
platform darwin -- Python 3.5.2, pytest-3.0.1, py-1.4.31, pluggy-0.3.1
rootdir: /project.test, inifile:
collected 0 items / 1 errors
====================================================================================== ERRORS =======================================================================================
______________________________________________________________________ ERROR collecting tests/test_optional.py ______________________________________________________________________
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/_pytest/python.py:209: in fget
return self._obj
E AttributeError: 'Module' object has no attribute '_obj'
During handling of the above exception, another exception occurred:
tests/test_optional.py:3: in <module>
def method(src: Optional(str)):
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/typing.py:135: in __new__
raise TypeError("Cannot instantiate %r" % self.__class__)
E TypeError: Cannot instantiate <class 'typing.OptionalMeta'>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================== 1 error in 0.20 seconds ==============================================================================
pip list:
$ pip3.5 list
pip (8.1.1)
py (1.4.31)
pytest (3.0.1)
setuptools (20.10.1)
This has nothing to do with pytest:
>>> from typing import Optional
>>> def foo(x: Optional(str)):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/typing.py", line 135, in __new__
raise TypeError("Cannot instantiate %r" % self.__class__)
TypeError: Cannot instantiate <class 'typing.OptionalMeta'>
Your annotation should be Optional[str], not Optional(str).
Oooops! Sorry for bothering! and thx for fast reply!
Most helpful comment
This has nothing to do with pytest:
Your annotation should be
Optional[str], notOptional(str).