Home > front end >  Failed to run pytest with allure on my docker image,"invalid syntax"
Failed to run pytest with allure on my docker image,"invalid syntax"

Time:09-17

I try to run pytest with allure on my docker image, but it reports an "invalid syntax" error. Is there any python version requirement for allure? The python version on my docker image is 2.7.13. Can anyone help me?

root@ubuntu:/fuego-rw/buildzone# pytest allure_title.py 
Traceback (most recent call last):
  File "/usr/local/bin/pytest", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 65, in main
    config = _prepareconfig(args, plugins)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 214, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/usr/local/lib/python2.7/dist-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/usr/local/lib/python2.7/dist-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/usr/local/lib/python2.7/dist-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/usr/local/lib/python2.7/dist-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 789, in pytest_cmdline_parse
    self.parse(args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 997, in parse
    self._preparse(args, addopts=addopts)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 943, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/usr/local/lib/python2.7/dist-packages/pluggy/manager.py", line 299, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/usr/local/lib/python2.7/dist-packages/importlib_metadata/__init__.py", line 105, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/assertion/rewrite.py", line 304, in load_module
    exec(co, mod.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/allure_pytest/plugin.py", line 3, in <module>
    import allure
  File "/usr/local/lib/python2.7/dist-packages/allure.py", line 1, in <module>
    from allure_commons._allure import title
  File "/usr/local/lib/python2.7/dist-packages/allure_commons/__init__.py", line 3, in <module>
    from allure_commons._allure import fixture  # noqa: F401
  File "/usr/local/lib/python2.7/dist-packages/allure_commons/_allure.py", line 165
    def __call__(self, func: _TFunc) -> _TFunc:
                           ^
SyntaxError: invalid syntax

CodePudding user response:

This syntax:

func: _TFunc

is called a type hint, meaning the variable func is expected to be of type _TFunc (in simpler terms, num: int means the variable num is expected to be int).

The type hint feature was only made available from Python3.0 (PEP 3107) and Python3.5 (PEP 484), thus isn't available in the version you are using which is Python2.7.

def func(num: int):
    print(num)

func(1)

Using Python2

  File "Main.py", line 1
    def func(num: int):
                ^
SyntaxError: invalid syntax

Using Python3

1

Either upgrade your Python version to >=3.5 or use an older version of allure-pytest. I would advise to upgrade Python as that would be more sustainable.

  • Related