akaihola / darker

Apply black reformatting to Python files only in regions changed since a given commit. For a practical usage example, see the blog post at https://dev.to/akaihola/improving-python-code-incrementally-3f7a
https://pypi.org/project/darker/
Other
628 stars 56 forks source link

Mypy doesn't load environment variables. #732

Closed artel1992 closed 4 weeks ago

artel1992 commented 1 month ago

I get an error when I run mypy through Darker.

command:

darker --diff --lint "mypy ."

stdout:

Loading .env environment variables...
Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning.
--- backend_settings/conf/selectel.py   2024-08-16 18:56:14.356079 +0000
+++ backend_settings/conf/selectel.py   2024-08-17 08:33:39.302140 +0000
@@ -1,6 +1,6 @@
 from backend_settings.conf.envconf import config

 SELECTEL_URL_NEW = config("SELECTEL_URL_NEW", "")
 CONTAINER_NAME_NEW = config("CONTAINER_NAME_NEW", "")
 AWS_ACCESS_KEY_ID = config("AWS_ACCESS_KEY_ID", "")
-AWS_SECRET_ACCESS_KEY = config("AWS_SECRET_ACCESS_KEY", '')
+AWS_SECRET_ACCESS_KEY = config("AWS_SECRET_ACCESS_KEY", "")
--- backend_settings/conf/telegram.py   2024-08-16 18:50:37.606703 +0000
+++ backend_settings/conf/telegram.py   2024-08-17 08:33:39.405115 +0000
@@ -1,4 +1,4 @@
 from backend_settings.conf.envconf import config

-TOKEN = config('TELEGRAM_TOKEN', '')
-CHANEL_ID = config('TELEGRAM_CHANEL_ID', '')
+TOKEN = config("TELEGRAM_TOKEN", "")
+CHANEL_ID = config("TELEGRAM_CHANEL_ID", "")
Traceback (most recent call last):
  File "C:\Users\voxak\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\voxak\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\Scripts\mypy.exe\__main__.py", line 7, in <module>
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\mypy\__main__.py", line 15, in console_entry
    main()
  File "mypy\main.py", line 103, in main
  File "mypy\main.py", line 187, in run_build
  File "mypy\build.py", line 193, in build
  File "mypy\build.py", line 238, in _build
  File "mypy\build.py", line 503, in load_plugins
  File "mypy\build.py", line 484, in load_plugins_from_config
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\mypy_django_plugin\main.py", line 65, in __init__
    self.django_context = DjangoContext(self.plugin_config.django_settings_module)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\mypy_django_plugin\django\context.py", line 98, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\mypy_django_plugin\django\context.py", line 81, in initialize_django
    settings._setup()  # type: ignore[misc]
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\django\conf\__init__.py", line 71, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\django\conf\__init__.py", line 179, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\voxak\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\voxak\AppData\Local\Temp\tmpab55x7c1\baseline-revision\monge_backend\./monge_project\backend_settings\__init__.py", line 2, in <module>
    from core.openapi.extensions import *
  File "C:\Users\voxak\AppData\Local\Temp\tmpab55x7c1\baseline-revision\monge_backend\./monge_project\core\openapi\__init__.py", line 12, in <module>
    from .extensions import ManyAsOneRelatedFieldExtension, ConstantFieldExtension, PrimaryKeyRelationFilterExtension
  File "C:\Users\voxak\AppData\Local\Temp\tmpab55x7c1\baseline-revision\monge_backend\./monge_project\core\openapi\extensions\__init__.py", line 1, in <module>
    from .constant_field import ConstantFieldExtension
  File "C:\Users\voxak\AppData\Local\Temp\tmpab55x7c1\baseline-revision\monge_backend\./monge_project\core\openapi\extensions\constant_field.py", line 1, in <module>
    from drf_spectacular.extensions import OpenApiSerializerFieldExtension
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\drf_spectacular\extensions.py", line 4, in <module>
    from drf_spectacular.plumbing import OpenApiGeneratorExtension
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\drf_spectacular\plumbing.py", line 35, in <module>
    from rest_framework import exceptions, fields, mixins, serializers, versioning
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\rest_framework\versioning.py", line 9, in <module>
    from rest_framework.templatetags.rest_framework import replace_query_param
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\rest_framework\templatetags\rest_framework.py", line 12, in <module>
    from rest_framework.renderers import HTMLFormRenderer
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\rest_framework\renderers.py", line 53, in <module>
    class JSONRenderer(BaseRenderer):
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\rest_framework\renderers.py", line 60, in JSONRenderer
    ensure_ascii = not api_settings.UNICODE_JSON
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\rest_framework\settings.py", line 218, in __getattr__
    val = self.user_settings[attr]
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\rest_framework\settings.py", line 209, in user_settings
    self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\django\conf\__init__.py", line 84, in __getattr__
    self._setup(name)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\django\conf\__init__.py", line 71, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\django\conf\__init__.py", line 179, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\voxak\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\voxak\AppData\Local\Temp\tmpab55x7c1\baseline-revision\monge_backend\./monge_project\backend_settings\settings.py", line 11, in <module>
    include(
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\split_settings\tools.py", line 106, in include
    exec(compiled_code, scope)  # noqa: S102, WPS421
  File "C:\Users\voxak\AppData\Local\Temp\tmpab55x7c1\baseline-revision\monge_backend\monge_project\backend_settings\conf\selectel.py", line 3, in <module>
    SELECTEL_URL_NEW = config("SELECTEL_URL_NEW")
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\decouple.py", line 248, in __call__
    return self.config(*args, **kwargs)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\decouple.py", line 107, in __call__
    return self.get(*args, **kwargs)
  File "C:\Users\voxak\.virtualenvs\monge_backend-AlCsxE3x\lib\site-packages\decouple.py", line 92, in get
    raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: SELECTEL_URL_NEW not found. Declare it as envvar or define a default value.

To Reproduce I'm not sure if this will be enough, but try installing all the specified dependencies and running the command through Pipenv

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add copy/paste the output or attach a screenshots to help explain your problem.

Environment

I use Pipenv to run commands.

[scripts]
start = "python monge_project/manage.py runserver"
migrate = "python monge_project/manage.py migrate"
collectstatic = "python monge_project/manage.py collectstatic --no-input"
"lint" = 'darker --diff --lint "mypy ."'
"lint:pylint" = 'pylint ./monge_project'
"lint:mypy" = 'mypy .'

lint:mypy - works okay

Additional context Unfortunately, I cannot provide the files without breaching confidentiality

artel1992 commented 1 month ago

pyproject.toml

[tool.darker]
src = [
    "./monge_project"
]
isort = true
target-version = "py310"

[tool.black]
force_exclude = '''
/(
    migrations
)/
'''

[tool.isort]
profile = "black"
known_third_party = ["pytest"]

[tool.pylint.'MAIN']
[tool.pylint.'BASIC']
[tool.pylint.'CLASSES']
[tool.pylint.'DESIGN']
[tool.pylint.'EXCEPTIONS']
[tool.pylint.'FORMAT']
[tool.pylint.'IMPORTS']
[tool.pylint.'LOGGING']
[tool.pylint.'MESSAGES CONTROL']
disable = [
    "raw-checker-failed",
    "bad-inline-option",
    "locally-disabled",
    "file-ignored",
    "suppressed-message",
    "useless-suppression",
    "deprecated-pragma",
    "use-symbolic-message-instead",
    "use-implicit-booleaness-not-comparison-to-string",
    "use-implicit-booleaness-not-comparison-to-zero",
    "missing-function-docstring",
    "missing-module-docstring",
    "missing-class-docstring"
]

[tool.mypy]
python_version = "3.10"
check_untyped_defs = true
mypy_path = "./monge_project"
plugins = [
    "mypy_django_plugin.main",
    "mypy_drf_plugin.main"
]
warn_unused_ignores = true

[[tool.mypy.overrides]]
module = "decouple"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "simple_history.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "dadata"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "rest_framework_recursive.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "sqlparse"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "user_agents"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "ipware"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "haystack.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "django_celery_beat.*"
ignore_missing_imports = true

[tool.django-stubs]
django_settings_module = "backend_settings.settings"
akaihola commented 3 weeks ago

@artel1992 thanks for reporting and closing – I'm curious whether you found out that the problem is elsewhere than in Darker, or did you close the issue for some other reason?