如何为另一个包打包类型存根?

How can I package type stubs for another package?

Python 包 Flask-HTTPAuth 有并且可能不会有类型注释(source). I would like to create them and provide them as a package on mypy, without forking the project. I've created a flask-httpauth-stubs package

我需要做什么来告诉 mypy 这个包提供了 Flask-HTTPAuth 的存根?

我如何测试

code.py:

from flask_httpauth import HTTPAuth

def autho(a: HTTPAuth):
    return a.get_auth()

然后:

$ pip install flask_httpauth flask_httpauth-stubs

$ mypy code.py

code.py:1: error: Skipping analyzing 'flask_httpauth': found module but no type hints or library stubs
code.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

我还尝试将包重命名为 flask_httpauth-stubs 以防 CASE 或 dash/underscore 重要。结果相同。

您的尝试在其他方面似乎很完美,但是在您的存根分发包安装的 Python 包的命名中存在一个小错误。由于代码位于名为“flask_httpauth”(带下划线)的 Python 包中,存根必须位于名为“flask_httpauth-stubs”的 Python 包中,即同名并附加“-stubs”。

我创建了一个修复您的存根包的拉取请求: https://github.com/MartinThoma/flask-httpauth-stubs/pull/1

我检查了你的示例 code.py 可以在安装存根时进行类型检查

$ mypy code.py 
Success: no issues found in 1 source file