我正在检查字符串是否以“ 4”开头,它将自己分类为签证卡。运行pytest -v后,返回语法错误。在我的validator.py中,我的函数如下所示:
def get_issuer(number: str) -> str:
if number.startswith('4'):
return 'Visa'
我正在单独的文件中对此进行测试:
from card_validator.validator import get_issuer
def test_get_issuer_visa():
assert get_issuer("4343 4212 1435 1231") == "Visa"
错误是
def get_issuer(number: str) -> str:
E ^
E SyntaxError: invalid syntax
我找不到任何错误。有什么帮助吗?
编辑: pytest -v的FULL输出为
========================================================== test session starts ==========================================================
platform linux2 -- Python 2.7.15rc1, pytest-3.3.2, py-1.5.2, pluggy-0.6.0 -- /usr/bin/python2
cachedir: .cache
rootdir: /home/bs-094/Dev/validator, inifile:
collected 0 items / 1 errors
================================================================ ERRORS =================================================================
_______________________________________________ ERROR collecting tests/test_validators.py _______________________________________________
/usr/lib/python2.7/dist-packages/_pytest/python.py:403: in _importtestmodule
mod = self.fspath.pyimport(ensuresyspath=importmode)
/usr/lib/python2.7/dist-packages/py/_path/local.py:668: in pyimport
__import__(modname)
/usr/lib/python2.7/dist-packages/_pytest/assertion/rewrite.py:213: in load_module
py.builtin.exec_(co, mod.__dict__)
tests/test_validators.py:1: in <module>
from card_validator.validator import get_issuer
E File "/home/bs-094/Dev/validator/card_validator/validator.py", line 1
E def get_issuer(number: str) -> str:
E ^
E SyntaxError: invalid syntax
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================== 1 error in 0.12 seconds ========================================================
答案 0 :(得分:2)
您的脚本包含属于python3的语法。因此,仅适用于python3。
要在python2.7(和python3中)中执行脚本,可以如下删除函数签名中的冒号:
def get_issuer(number):
if number.startswith('4'):
return 'Visa'
答案 1 :(得分:1)
正如@Mark Tolonen在评论中提到的那样,键入是3.5版中的新增内容 https://docs.python.org/3/library/typing.html
您的代码在3.7.3中运行良好。升级到3.5+应该可以解决您的问题。