ecugol / pre-commit-hooks-django

Some useful hooks for Django development
MIT License
35 stars 11 forks source link

Have to manually enable venv before commiting, or mirror django and all other dependencies into additional_dependencies #4

Open leinadao opened 2 years ago

leinadao commented 2 years ago

I use poetry and have the following dependencies:

[tool.poetry.dependencies]
python = "^3.8.9"
Django = "^4.0"
django-bootstrap-v5 = "^1.0.7"

The entrypoint commands in check-unapplied-migrations and check-absent-migrations simply run 'python' which fails importing django and / or (depending on the system python packages installed) django-bootstrap-v5. These are required via manage.py and e.g. Django's INSTALLED_APPS.

Manually activating the virtual environment (e.g. poetry shell) before commiting is one solution.

Based on some progress, I believe it would be possible to resolve by adding every dependency to the hook's additional_dependencies, but this is horribly repetative and error-prone.

Instead I've temporarily resolved the issue by overriding the hook's entrypoint to use poetry and so invoke the virtual environment and have access to all dependencies required to run the project's migration code.

-   repo: https://github.com/ecugol/pre-commit-hooks-django
    rev: v0.4.0
    hooks:
    -   id: check-untracked-migrations
    -   id: check-unapplied-migrations
        entry: sh -c 'poetry run python `find . -name "manage.py"` migrate --check'
    -   id: check-absent-migrations
        entry: sh -c 'poetry run python `find . -name "manage.py"` makemigrations --check --dry-run'

Perhaps configuration could be added for which 'python' command to run? And / or cases like this could be added to the documentation.

panteparak commented 5 months ago

might be a better idea if we could specify a way to point each hook to the right python executable within virtual environment using command or direct path

in the case of poetry

-   repo: https://github.com/ecugol/pre-commit-hooks-django
    rev: v0.4.0
    hooks:
    -   id: check-absent-migrations
        python: 'poetry run python'

in the case of a custom python binary path

-   repo: https://github.com/ecugol/pre-commit-hooks-django
    rev: v0.4.0
    hooks:
    -   id: check-absent-migrations
        python: '/usr/<your-custom-python-path>/bin/python3'

and of course, we can default python executable path to the one presented in the system executable path