简体   繁体   English

如何让 mypy 告诉我我正在使用旧 Python 版本不支持的功能?

[英]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 似乎已经可以按预期使用内置类型,但在其他情况下会失败,例如上面的Futurequeue.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.

相关问题 如何从旧的Python版本中隐藏不兼容的代码? - How can I hide incompatible code from older Python versions? 在旧版本的 Python 上使用现代打字功能 - Using modern typing features on older versions of Python 如何让 mypy 记住 hasattr? - How can I make mypy remember hasattr? 如何让 pyautogui 告诉我 locateOnScreen function 的坐标 - How can I make pyautogui tell me the coordinates of the locateOnScreen function 这个给定的文本是 json 格式。因为我是 python 的新手,所以告诉我如何只从用户那里获取 id - This given text is in json format.As i am new to python so tell me how can i get id from user only 如何告诉MyPy参数不是可选的? - How do I tell MyPy that a parameter is not optional? 我收到 TypeError: can only concatenate str (not "NoneType") to str while using tkinter,请告诉我如何解决这个问题 - I am getting TypeError: can only concatenate str (not "NoneType") to str while using tkinter, please tell me how can fix this 我应该如何构建我的 Python 模块,以便将来可以随时运行旧版本? - How should I structure my Python module so that I can run older versions at any time in the future? 有人可以告诉我我做错了什么新到 Python - Can someone please tell me what am I doing wrong New to Python 谁能告诉我我做错了什么,字典与 Python 中的列表? - Can anyone tell me what I am doing wrong, dictionary vs list in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM