Django和Mypy在子文件夹中使用应用程序
我们有一个带有以下设置的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,这是因为
__ Init __. py
文件。您是否在应用程序文件夹中添加了
__ INIT __。py
文件?I think, it's because of
__init__.py
file.Did you add
__init__.py
file in your apps folder?