5

I'm trying to automate my tests and code static analysis with pre-commit.My .pre-commit-config.yaml is like below:

# .
# .
# .
- repo: https://github.com/pre-commit/mirrors-mypy
    rev: 'v0.910'
    hooks:
      - id: mypy
        args: [--no-strict-optional, --ignore-missing-imports]
        exclude: "[a-zA-Z]*/[a-zA-Z]*/(migrations)/(.)*"
        additional_dependencies: [
            'tokenize-rt,
            'djangorestframework-stubs',
            'django-stubs',
        ]

The pyproject.toml is:

[tool.mypy]
python_version = "3.8"
plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]

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

When I run the pre-commit run --all-files I get the following error:

Error constructing plugin instance of NewSemanalDjangoPlugin

Traceback (most recent call last):
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 87, in main
  File "mypy/main.py", line 165, in run_build
  File "mypy/build.py", line 179, in build
  File "mypy/build.py", line 229, in _build
  File "mypy/build.py", line 475, in load_plugins
  File "mypy/build.py", line 453, in load_plugins_from_config
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/mypy_django_plugin/main.py", line 104, in __init__
    self.django_context = DjangoContext(django_settings_module)
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/mypy_django_plugin/django/context.py", line 88, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/mypy_django_plugin/django/context.py", line 72, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/django/apps/config.py", line 224, in create
    import_module(entry)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'

But I have installed both djangoresetframework and djangorestframework-stubs in my environment. What is the problem here?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Alipqb
  • 101
  • 4
  • 7

1 Answers1

6

pre-commit runs its tools in isolated environments, you can control the dependencies (as you've seen already it seems (!)) from the additional_dependencies as stated in the mirrors-mypy readme

it looks like the mypy_django_plugin is missing dependencies -- notably it tries to import django and rest-framework so you'll need to make sure to include those inside additional_dependencies

note that you don't need tokenize-rt -- that was just an example from the README so you can remove that


disclaimer: I created pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thanks for the answer. I can run `mypy` manually but with `pre-commit` I get this error. I know the problem is with settings in `pyproject.toml` or the `.pre-commit-config.yaml` but I don't know the real location of error. I have also read the documents for all of them and I added all possible configurations. I don't know what to do anymore. – Alipqb Jul 11 '21 at 19:59
  • 1
    oh sorry if it wasn't clear, the tl;dr is add the missing things to `additional_dependencies` – anthony sottile Jul 11 '21 at 21:45
  • Once I add additional dependencies then mypy does not detect any of the packages installed in the django project – narravabrion Jul 26 '23 at 19:25