如何让 .env 进行预提交 + mypy + Django 存根
我尝试在提交之前配置 start mypy + django-stubs 检查。我使用预提交。 当我尝试提交时,出现错误django.core.exceptions.ImproperlyConfigured:设置POSTGRES_DB环境变量。该变量位于 .env 文件中,我使用 django-environ 将变量从 .env 导出到 Django 配置。当然还有 .gitignore 中的 .env。所以,据我了解:预提交从它自己的虚拟环境开始,它不知道我的 .env 文件。
最重要的是,我是否正确理解了我的情况?如果我是对的,如何将变量从 .env 文件获取到预提交环境?
我的.pre-commit-config.yaml(部分)
- repo: https://github.com/pre-commit/mirrors-mypy
rev: ''
hooks:
- id: mypy
exclude: "[a-zA-Z]*/(migrations)/(.)*"
args: [--config=setup.cfg,
--no-strict-optional,
--ignore-missing-imports]
additional_dependencies: [django-stubs, django-environ]
我的setup.cfg
[mypy]
python_version = 3.9
allow_redefinition = True
check_untyped_defs = True
ignore_missing_imports = True
incremental = True
strict_optional = True
show_traceback = True
warn_no_return = False
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
plugins = mypy_django_plugin.main
show_column_numbers = True
[mypy.plugins.django-stubs]
django_settings_module = config.settings.local
[mypy_django_plugin]
ignore_missing_model_attributes = True
[mypy-*.migrations.*]
# Django migrations should not produce any errors:
ignore_errors = True
I try to configurate start mypy + django-stubs cheking before commiting. I use pre-commit for it.
When I try to commit, I have error django.core.exceptions.ImproperlyConfigured: Set the POSTGRES_DB environment variable. This variable is in .env file, I export variables from .env to Django config using django-environ. And of course .env in .gitignore. So, as far as I understand: pre-commit starts with its own virtual environment, and it doesn't know about my .env file.
Above all, do I correct to understand my situation? If I'm right, how to get variables from .env file to pre-commit enviroment?
My .pre-commit-config.yaml (part)
- repo: https://github.com/pre-commit/mirrors-mypy
rev: ''
hooks:
- id: mypy
exclude: "[a-zA-Z]*/(migrations)/(.)*"
args: [--config=setup.cfg,
--no-strict-optional,
--ignore-missing-imports]
additional_dependencies: [django-stubs, django-environ]
my setup.cfg
[mypy]
python_version = 3.9
allow_redefinition = True
check_untyped_defs = True
ignore_missing_imports = True
incremental = True
strict_optional = True
show_traceback = True
warn_no_return = False
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
plugins = mypy_django_plugin.main
show_column_numbers = True
[mypy.plugins.django-stubs]
django_settings_module = config.settings.local
[mypy_django_plugin]
ignore_missing_model_attributes = True
[mypy-*.migrations.*]
# Django migrations should not produce any errors:
ignore_errors = True
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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