Django和Mypy在子文件夹中使用应用程序

发布于 2025-02-06 13:38:26 字数 921 浏览 0 评论 0原文

我们有一个带有以下设置的Django项目:

我们在apps文件夹中都有我们的应用程序。我们将所有这些应用程序添加到settings.py级别的Python路径。

然后,我们可以像这样引用和导入这些应用程序:

from apps.booking.models import Booking
# or
from booking.models import Booking

现在,我们想将mypy用于类型检查目的。

问题是MyPy无法从booking.models导入预订并抱怨此模块缺少存根。这使得键入非常不可靠,并且不是很有用,因为预订然后将其键入为任何。它可以通过Apps.booking.models导入预订

有没有办法使Mypy理解没有前缀 apps。的方法??

我尝试使用mypy_path = $ mypy_config_file_dir/apps in mypy.ini,但没有任何成功...

编辑:

为了清楚起见,这是我的完整mypy。 ini:

[mypy]
mypy_path = $MYPY_CONFIG_FILE_DIR/apps
show_error_codes = true
sqlite_cache = true
plugins =
    mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = "myapp.settings"

We have a Django project with the following setup:

We have our apps in an apps folder. We added all those apps to the python path at settings.py level.

We then can refer and import these apps like so:

from apps.booking.models import Booking
# or
from booking.models import Booking

Now, we want to use mypy for type checking purposes.

The issue is that mypy does not recognize the import from booking.models import Booking and complains about missing stubs for this module. This make the typing very unreliable and not very useful as Booking is then typed as Any. It works fine with an import as follow however from apps.booking.models import Booking

Is there a way to make mypy understand the path without the prefix apps. ?

I tried to use mypy_path = $MYPY_CONFIG_FILE_DIR/apps in mypy.ini but without any success...

Edit:

For clarity, here is my full mypy.ini:

[mypy]
mypy_path = $MYPY_CONFIG_FILE_DIR/apps
show_error_codes = true
sqlite_cache = true
plugins =
    mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = "myapp.settings"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2025-02-13 13:38:26

我认为,这是因为__ Init __. py文件。
您是否在应用程序文件夹中添加了__ INIT __。py文件?

I think, it's because of __init__.py file.
Did you add __init__.py file in your apps folder?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文