Poetry+mypy 忽略导入模块,而 Poetry+python 工作正常
我确实搞砸了我的 python 包设置,尽管 python 本身运行良好,但无法使用 mypy 正确导入包,但我不太清楚解决方案是什么。我已经能够用非常简单的方法重现这个问题。
然后安装程序
poetry new foo
cd foo
poetry add mypy@latest
poetry install
poetry shell
创建以下文件结构:
pyproject.toml
poetry.lock
foo/
__init__.py
a.py
bar/
__init__.py
b.py
# a.py
from foo.bar import b
will_fail: int = "a.py string"
print(will_fail)
# b.py
will_fail: int = "b.py string"
print(will_fail)
正如预期的那样,运行python foo/a.py
会执行a.py和b.py并打印这两个语句。 运行 mypy foo
会给出预期的错误:
foo/bar/b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int")
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 2 errors in 2 files (checked 4 source files)
问题
但是运行 mypy foo/a.py
only 会给出:
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
运行 mypy foo/a.py --follow-imports=error
给出:
foo/a.py: error: Ancestor package "foo" ignored
foo/a.py: note: (Using --follow-imports=error, submodule passed on command line)
foo/a.py:1: error: Import of "foo.bar.b" ignored
foo/a.py:1: note: (Using --follow-imports=error, module not passed on command line)
foo/a.py:1: error: Import of "foo.bar" ignored
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 4 errors in 1 file (checked 1 source file)
问题
那么为什么 mypy 不 进入模块并在导入时对它们进行类型检查?
我在使用诗歌的方式上是否犯了一些严重的错误?我的? Python?
我已经阅读了 mypy 文档一百万次(https ://mypy.readthedocs.io/en/stable/running_mypy.html#how-mypy-handles-imports),似乎正在发生的事情是一些“第四”选项从那里描述的3?
I've definitely botched my python package setup in a way that is failing to properly import packages with mypy despite running fine with python itself, but I can't quite tell how or what the solution is. I've been able to repro the problem with a pretty minimal approach.
The Setup
poetry new foo
cd foo
poetry add mypy@latest
poetry install
poetry shell
then create the following file structure:
pyproject.toml
poetry.lock
foo/
__init__.py
a.py
bar/
__init__.py
b.py
# a.py
from foo.bar import b
will_fail: int = "a.py string"
print(will_fail)
# b.py
will_fail: int = "b.py string"
print(will_fail)
As expected running python foo/a.py
executes both a.py and b.py and prints both statements.
And running mypy foo
gives the expected errors:
foo/bar/b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int")
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 2 errors in 2 files (checked 4 source files)
The Problem
But running mypy foo/a.py
only gives:
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
And running mypy foo/a.py --follow-imports=error
gives:
foo/a.py: error: Ancestor package "foo" ignored
foo/a.py: note: (Using --follow-imports=error, submodule passed on command line)
foo/a.py:1: error: Import of "foo.bar.b" ignored
foo/a.py:1: note: (Using --follow-imports=error, module not passed on command line)
foo/a.py:1: error: Import of "foo.bar" ignored
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 4 errors in 1 file (checked 1 source file)
The Question
So why is mypy not following into the modules and type checking them as they are imported?
Am I making some critical mistake in how I'm using poetry? mypy? python?
I've read the mypy docs a million times (https://mypy.readthedocs.io/en/stable/running_mypy.html#how-mypy-handles-imports) and it seems like what's happening is some "fourth" option from the 3 described there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论