Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-commit hook doesn't recognize internal packages #103

Open
zeraye opened this issue Nov 14, 2024 · 6 comments
Open

Pre-commit hook doesn't recognize internal packages #103

zeraye opened this issue Nov 14, 2024 · 6 comments

Comments

@zeraye
Copy link

zeraye commented Nov 14, 2024

Pre-commit hook doesn't recognize internal packages. When I lint project using ruff check inside superproject directory everything works fine. But when I run pre-commit, imports from external and internal packages (eg. flask and superpackage) are not separated. This happens because pre-commit doesn't recognize internal and external packages. I tried to cd superproject && ruff check, but this doesn't seem to work. Other solutions with bash -c 'cd ...' seems way too hacky. I also don't want to store inside pyproject.toml list of all internal packages. Is there any elegant solution?

Project structure:

.
└── superproject
    ├── main.py
    ├── pyproject.toml
    └── superpackage
        ├── __init__.py
        ├── __pycache__
        └── test.py

main.py

from flask import json

from superpackage.test import hello_world

# this doesn't matter
print(hello_world())
print(json.dumps({"hello": "world"}))

pyproject.toml

[tool.ruff.lint]
extend-select = ["I"]

[tool.ruff.lint.isort]
combine-as-imports = true

Python 3.12.3
Ruff 0.7.3

@dhruvmanila
Copy link
Member

Can you specify where your .pre-commit-config.yaml file lives? Is it in a directory above superproject or is it inside superproject?

@zeraye
Copy link
Author

zeraye commented Nov 18, 2024

Can you specify where your .pre-commit-config.yaml file lives? Is it in a directory above superproject or is it inside superproject?

Above superproject

@skullyhoofd
Copy link

running into the same issue, where the pre-commit yaml also lives above the root of our logic.

@zeraye
Copy link
Author

zeraye commented Nov 28, 2024

@skullyhoofd if you want an obscure solution:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.7.3
    hooks:
      - id: ruff
        name: Python Ruff linter for superproject
        entry: bash -c 'cd superproject && ruff check . --config pyproject.toml --fix'
        language_version: python3.12
        types: [python]
        files: "superproject"
      - id: ruff-format
        name: Python Ruff formatter for superproject
        entry: bash -c 'cd superproject && ruff format . --config pyproject.toml'
        language_version: python3.12
        types: [python]
        files: "superproject"

That's the best solution I'm aware of. Hate this, but it works

@dhruvmanila
Copy link
Member

So, just to confirm that this is your project structure?

.
├── .git
|   └── ...
├── .pre-commit-config.yaml
└── superproject
    ├── main.py
    ├── pyproject.toml
    └── superpackage
        ├── __init__.py
        └── test.py

Can you specify how you're running pre-commit? Which directory are you running it from?

For me, it doesn't seem to be complaining:

/tmp/ruff-pre-commit-test
$ pre-commit run --all-files
ruff.....................................................................Passed

/tmp/ruff-pre-commit-test
$ cd superproject

/tmp/ruff-pre-commit-test/superproject
$ pre-commit run --all-files
ruff.....................................................................Passed

@zeraye
Copy link
Author

zeraye commented Dec 9, 2024

@dhruvmanila

NOTE: pre-commit-config in the comment above is my current workaround

Your project structure is correct. To be more precise:

test.py

def hello_world():
    print("Hello, world!")

.pre-commit-config.yaml

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.7.3
    hooks:
      - id: ruff
        name: Python Ruff linter for superproject
        args: ["--config", "superproject/pyproject.toml", "--fix"]
        language_version: python3.12
        types: [python]
        files: "superproject"
      - id: ruff-format
        name: Python Ruff formatter for superproject
        args: ["--config", "superproject/pyproject.toml"]
        language_version: python3.12
        types: [python]
        files: "superproject"
❯ pre-commit run --all-files
Python Ruff linter for superproject......................................Failed
- hook id: ruff
- files were modified by this hook

Found 1 error (1 fixed, 0 remaining).

Python Ruff formatter for superproject...................................Passed

This is how ruff formatted main.py

from flask import json
from superpackage.test import hello_world

# this doesn't matter
print(hello_world())
print(json.dumps({"hello": "world"}))

I want this file to be formatted as in my issue description. Ruff doesn't recognize here external (flask) and internal (superpackage) libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants