__init__.py 的要求只是为了满足 pylint 和 mypy
Requirement for __init__.py just to satisfy pylint and mypy
我有一个具有以下(部分)目录结构的项目
.
├── mypy.ini
├── src
│ ├── preppy
│ │ ├── cli.py
│ │ ├── __main__.py
│ │ ├── model.py
│ │ └── tools.py
├── pyproject.toml
└── tests
在 cli.py
中,我有以下代码(文件中的第 13 和 14 行):
from .model import Problem
from .tools import get_abs_path, transcode
我在 model.py
和 __main__.py
中也有类似样式的相对导入
当工具在我的 IDE(代码 - OSS)中自动 运行 时,所有类似的导入都会在 pylint
(2.5.3) 和 mypy
(0.761) 中抛出错误,例如:
Attempted relative import beyond top-level package pylint(relative-beyond-top-level) [13,1]
Cannot find implementation or library stub for module named '.model' mypy(error) [13,1]
Attempted relative import beyond top-level package pylint(relative-beyond-top-level) [14,1]
Cannot find implementation or library stub for module named '.tools' mypy(error) [14,1]
See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports mypy(note) [13,1]
当我将空白 __init__.py
文件添加到文件夹时,错误消失了。
我不需要这个 __init__.py
文件来让包工作。
我认为 post-PEP 420 不应该是必需的,特别是如果它只是为了满足 linters 的话。
还有什么地方我做错了吗,还是我应该添加 __init__.py
然后克服它:)?
pylint
的配置在 pyproject.toml
:
[tool.pylint.'MESSAGES CONTROL']
# Pylint and black disagree on hanging indentation.
disable = "C0330"
[tool.pylint.MISCELLANEOUS]
# Note: By default, "TODO" is flagged, this is disabled by omitting it
# from the list below.
notes = "FIXME,XXX"
mypy
的配置在 mypy.ini
:
[mypy]
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
mypy_path = src
namespace_packages = True
[mypy-openpyxl]
ignore_missing_imports = True
[mypy-pulp]
ignore_missing_imports = True
[mypy-pytest]
ignore_missing_imports = True
我是 运行宁 python 3.8.0.
PEP 420 不允许允许“通过省略__init__.py
创建一个包”,它强制“创建一个命名空间 包省略 __init__.py
”。这意味着:
- 如需套餐,请添加
__init__.py
。
- 如果您想要 namespace 包,请省略
__init__.py
。
虽然像常规包一样使用命名空间包通常是可行的,但当包名冲突时它可能会意外失败。在大多数情况下,命名空间包是不可取的。
对于 mypy,已接受答案的替代方法是使用 mypy 选项 --namespace-packages
,以便 mypy
考虑命名空间包
我有一个具有以下(部分)目录结构的项目
.
├── mypy.ini
├── src
│ ├── preppy
│ │ ├── cli.py
│ │ ├── __main__.py
│ │ ├── model.py
│ │ └── tools.py
├── pyproject.toml
└── tests
在 cli.py
中,我有以下代码(文件中的第 13 和 14 行):
from .model import Problem
from .tools import get_abs_path, transcode
我在 model.py
和 __main__.py
中也有类似样式的相对导入
当工具在我的 IDE(代码 - OSS)中自动 运行 时,所有类似的导入都会在 pylint
(2.5.3) 和 mypy
(0.761) 中抛出错误,例如:
Attempted relative import beyond top-level package pylint(relative-beyond-top-level) [13,1]
Cannot find implementation or library stub for module named '.model' mypy(error) [13,1]
Attempted relative import beyond top-level package pylint(relative-beyond-top-level) [14,1]
Cannot find implementation or library stub for module named '.tools' mypy(error) [14,1]
See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports mypy(note) [13,1]
当我将空白 __init__.py
文件添加到文件夹时,错误消失了。
我不需要这个 __init__.py
文件来让包工作。
我认为 post-PEP 420 不应该是必需的,特别是如果它只是为了满足 linters 的话。
还有什么地方我做错了吗,还是我应该添加 __init__.py
然后克服它:)?
pylint
的配置在 pyproject.toml
:
[tool.pylint.'MESSAGES CONTROL']
# Pylint and black disagree on hanging indentation.
disable = "C0330"
[tool.pylint.MISCELLANEOUS]
# Note: By default, "TODO" is flagged, this is disabled by omitting it
# from the list below.
notes = "FIXME,XXX"
mypy
的配置在 mypy.ini
:
[mypy]
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
mypy_path = src
namespace_packages = True
[mypy-openpyxl]
ignore_missing_imports = True
[mypy-pulp]
ignore_missing_imports = True
[mypy-pytest]
ignore_missing_imports = True
我是 运行宁 python 3.8.0.
PEP 420 不允许允许“通过省略__init__.py
创建一个包”,它强制“创建一个命名空间 包省略 __init__.py
”。这意味着:
- 如需套餐,请添加
__init__.py
。 - 如果您想要 namespace 包,请省略
__init__.py
。
虽然像常规包一样使用命名空间包通常是可行的,但当包名冲突时它可能会意外失败。在大多数情况下,命名空间包是不可取的。
对于 mypy,已接受答案的替代方法是使用 mypy 选项 --namespace-packages
,以便 mypy