如何让 .env 进行预提交 + mypy + Django 存根

发布于 2025-01-10 18:37:49 字数 1341 浏览 5 评论 0原文

我尝试在提交之前配置 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文