[英]How can I make mypy tell me I am using features that are unsupported in older Python versions?
As an example, executing this code with Python versions older than 3.9 will raise an exception:例如,使用 Python 早于 3.9 的版本执行此代码将引发异常:
from concurrent.futures import Future
f: Future[int]
TypeError: 'type' object is not subscriptable TypeError: 'type' object 不可订阅
But mypy will not complain (I'm using mypy 0.910):但是 mypy 不会抱怨(我使用的是 mypy 0.910):
Success: no issues found in 1 source file成功:在 1 个源文件中未发现问题
Explicitly specifiying the Python version that mypy will use (eg --python-version=3.8
) does not change this.明确指定 mypy 将使用的 Python 版本(例如--python-version=3.8
)不会改变这一点。
Numerous times I have fallen into the trap of writing code that uses features from newer Python versions, assuming that mypy will tell me if I made any errors, to then discover these errors only later at runtime.很多次我都陷入了使用较新 Python 版本的功能编写代码的陷阱,假设 mypy 会告诉我我是否犯了任何错误,然后仅在运行时才发现这些错误。
How can I tell mypy to not assume features from certain Python versions (which I don't even have installed) to exist?我怎样才能告诉 mypy 不假设某些 Python 版本(我什至没有安装)的功能存在?
I'll take a guess: by running it with older mypy versions.我会猜测:通过使用较旧的 mypy 版本运行它。
You could set up CI to run with every mypy version you require, and locally use virtual envs for each version您可以将 CI 设置为与您需要的每个 mypy 版本一起运行,并在本地为每个版本使用虚拟环境
Mypy seems to already work as expected with built-in types, but fails in other cases, like Future
above, or queue.Queue
. Mypy 似乎已经可以按预期使用内置类型,但在其他情况下会失败,例如上面的Future
或queue.Queue
。
After reading Mypy docs – Annotation issues at runtime and Issue #7907 – Implement PEP 585 I'm not sure if this is a bug or intentional.在阅读了 Mypy 文档——运行时的注释问题和问题#7907——实施 PEP 585之后,我不确定这是一个错误还是故意的。 Maybe the assumption is that the runtime errors can be avoided by using from __future__ import annotations
.也许假设是可以通过使用from __future__ import annotations
来避免运行时错误。
After reading this answer it might be that this is a flaw in the Python standard library, or rather its typeshed, which promises more than is actually implemented, and mypy can't do anything about it.阅读此答案后,这可能是 Python 标准库中的一个缺陷,或者更确切地说是它的类型,它承诺的比实际实现的要多,而 mypy 对此无能为力。
Works:作品:
# example1.py
x: list[int]
$ mypy --python-version=3.8 example1.py
example1.py:1: error: "list" is not subscriptable, use "typing.List" instead
$ mypy --python-version=3.9 example1.py
Success: no issues found in 1 source file
Works:作品:
# example2.py
d: dict[str, int]
$ mypy --python-version=3.8 example2.py
example2.py:1: error: "dict" is not subscriptable, use "typing.Dict" instead
$ mypy --python-version=3.9 example2.py
Success: no issues found in 1 source file
Fails:失败:
# example3.py
from queue import Queue
q: Queue[int]
$ mypy --python-version=3.8 example3.py
Success: no issues found in 1 source file
$ python3.8 example3.py
Traceback (most recent call last):
File "example3.py", line 2, in <module>
q: Queue[int]
TypeError: 'type' object is not subscriptable
Workaround:解决方法:
# example4.py
from __future__ import annotations
from queue import Queue
q: Queue[int]
print('ok')
$ mypy --python-version=3.8 example4.py
Success: no issues found in 1 source file
$ python3.8 example4.py
ok
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.