基于 PEP 604 上面的代码应该通过 pylint 检查:
# test.py
def f1(a: int | str):
print(a)
但是我得到的结果是:
$ pylint test.py
************* Module test
test.py:3:10: E1131: unsupported operand type(s) for | (unsupported-binary-operation)
-----------------------------------
Your code has been rated at 0.00/10
导入注解不会遇到这个问题:
# test.py
from __future__ import annotations
def f1(a: int | str):
print(a)
$ pylint test.py TestPlatform [ hero6api/external_device][!?⇡][ v3.12.0(.venv)]
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 0.00/10, +10.00)
[环境]
蟒蛇版本:3.12
木柱:3.03
这个问题还没有解决吗?或者我犯了一些错误。感谢您回复我!
[已关闭] 我检查了我的工作空间,发现里面有
.pylintrc
。有一行带有 py-version=3.8
。只需编辑这一行即可。