mypy {{cookiecutter.project_slug}} 不是有效的 Python 包名称

问题描述 投票:0回答:1

我正在为 python 包构建 cookiecutter 模板,并且我想通过预提交对模板存储库本身进行一系列检查。

回购协议的框架如下所示:

my_cookiecutter_template
|   .pre-commit-config.yaml
|   cookiecutter.json
|
|___{{cookiecutter.project_slug}}
    |    pyproject.toml
    | 
    |____{{cookiecutter.project_slug}}
         |   __init__.py

我使用的预提交挂钩之一是 mypy,但它因以下错误而失败:

{{cookiecutter.project_slug}} is not a valid Python package name

因为目录中有一个名为

__init__.py
{{cookiecutter.project_slug}}
文件(在实例化模板时显然会用有效的名称重命名)。

我的问题是,如何抑制这个 mypy 错误? mypy 文档包含许多异常的详细信息以及特定的错误代码,这为您提供了抑制它的方法。但是,除非我弄错了,否则没有任何内容与这个特定错误相关

python mypy cookiecutter
1个回答
0
投票

我必须将以下内容添加到 pre-commit-config.yaml 中:

# See https://pre-commit.com for more information
 # See https://pre-commit.com/hooks.html for more hooks
 repos:
    -   id: mypy
         # See https://github.com/pre-commit/mirrors-mypy/issues/1
         # This seems like the simplest way to avoid the mypy error
         # {{cookiecutter.project_slug}} is not a valid Python package name
         exclude: '.*__init__\.py'

通常,从 mypy 中排除某些文件的方法是将以下内容添加到任何 mypy 配置文件中(source):

[mypy]
exclude = __init__.py
#alternative
#exclude = (?x)(
#    __init__.py$
#    )

考虑到预提交的工作原理,使用此设置并不那么容易。请注意,此解决方案使用的是预提交本身的排除,而不是 mypy 排除。

© www.soinside.com 2019 - 2024. All rights reserved.