ruff-pre-commit
ruff-pre-commit copied to clipboard
[v0.6.9] I001 error on pre-commit, but not on CLI check
I have those imports like this in my file:
import asyncio
from enum import StrEnum
from typing import Any
from langchain.chains.llm import LLMChain
from langchain.docstore.document import Document
from app.generators.utils import (
convert_str_to_json,
get_llm,
)
# more local imports from app module...
a = 5
I don't have project.toml file.
I'm using pre-commit hooks with settings:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.9
hooks:
# Run the linter.
- id: ruff
args: [--select, I]
# Run the formatter.
- id: ruff-format
When I check the code in CLI I receive no errors:
ruff check --select I filename.py
All checks passed!
But when I try to commit changes in this file I receive error I001:
19 | | from app.generators.utils import (
20 | | convert_str_to_json,
21 | | get_llm,
22 | | )
27 | |
28 | | a = 5
| |_^ I001
|
= help: Organize imports
Python version 3.11. MacOS. This is really weird as I receive no similar error in any other file in my project... Thanks for help!
One more thing - the file looks like this after the precommit sorting if I use --fix flag:
import asyncio
from enum import StrEnum
from typing import Any
from app.generators.utils import (
convert_str_to_json,
get_llm,
)
from langchain.chains.llm import LLMChain
from langchain.docstore.document import Document
It looks like it treats langchain as local module. I have langchain_utils local module, but it is entirely different name.
Sorry for the delay. How do you run Ruff from the CLI? Are you also passing --select I? If not, then the reason why pre-commit raises an error and the CLI doesn't is because the pre-commit runs with import sorting on where the CLI does not (I is not in the default rule set)
FWIW, I'm seeing the same error when running the pre-commit hook and the CLI:
$ pre-commit run --files src/play.py
ruff.....................................................................Failed
- hook id: ruff
- exit code: 1
ruff/src/play.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import asyncio
2 | | from enum import StrEnum
3 | | from typing import Any
4 | |
5 | | from langchain.chains.llm import LLMChain
6 | | from langchain.docstore.document import Document
7 | |
8 | | from app.generators.utils import (
9 | | convert_str_to_json,
10 | | get_llm,
11 | | )
12 | | # more local imports from app module...
| |_^ I001
13 |
14 | a = 5
|
= help: Organize imports
Found 1 error.
[*] 1 fixable with the `--fix` option.
$ ruff check --select I src/play.py
src/play.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import asyncio
2 | | from enum import StrEnum
3 | | from typing import Any
4 | |
5 | | from langchain.chains.llm import LLMChain
6 | | from langchain.docstore.document import Document
7 | |
8 | | from app.generators.utils import (
9 | | convert_str_to_json,
10 | | get_llm,
11 | | )
12 | | # more local imports from app module...
| |_^ I001
13 |
14 | a = 5
|
= help: Organize imports
Found 1 error.
[*] 1 fixable with the `--fix` option.
@MichaReiser yeah, I do include --select I in CLI command.
I've tried the same as @dhruvmanila and got different results. I'm not sure what can cause it. My pre-commit config can be seen in original post (I'm using 0.6.9 in pre-commit as well).
(venv) ➜ src pre-commit run --files file.py
ruff.....................................................................Failed
- hook id: ruff
- exit code: 1
src/file.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import asyncio
2 | | from enum import StrEnum
3 | | from typing import Any
4 | |
5 | | from langchain.chains.llm import LLMChain
6 | | from langchain.docstore.document import Document
7 | |
8 | | from app.generators.utils import (
9 | | convert_str_to_json,
10 | | get_llm,
11 | | )
12 | |
13 | | # more local imports from app module...
| |_^ I001
14 |
15 | a = 5
|
= help: Organize imports
Found 1 error.
[*] 1 fixable with the `--fix` option.
ruff-format..............................................................Passed
(venv) ➜ src ruff check --select I file.py
All checks passed!
(venv) ➜ src ruff --version
ruff 0.6.9
(venv) ➜ src python --version
Python 3.11.9
On another note - it feels like error in formatter as langchain is not local module so it should be in "libraries" group of imports. And it looks like it is unable to differentiate between them - at least while running from pre-commit
Interesting. Could you try running ruff with ruff check --select I -v file.py and share the logs?
Here you go:
$ ruff check --select I -v demos/test.py
[2024-10-28][10:44:25][ruff::resolve][DEBUG] Using Ruff default settings
[2024-10-28][10:44:25][ruff::commands::check][DEBUG] Identified files to lint in: 4.992084ms
[2024-10-28][10:44:25][ruff::diagnostics][DEBUG] Checking: <PATH>/demos/test.py
[2024-10-28][10:44:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'asyncio' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:44:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'enum' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:44:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'typing' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:44:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'langchain.chains.llm' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:44:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'langchain.docstore.document' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:44:25][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'app.generators.utils' as Known(FirstParty) (SourceMatch("<MY_MODULE_PATH>"))
[2024-10-28][10:44:25][ruff::commands::check][DEBUG] Checked 1 files in: 9.37775ms
All checks passed!
And from pre-commit:
$ git commit -m "test commit"
[WARNING] Unstaged files detected.
ruff.....................................................................Failed
- hook id: ruff
- files were modified by this hook
[2024-10-28][10:46:28][ruff::resolve][DEBUG] Using Ruff default settings
[2024-10-28][10:46:28][ruff::commands::check][DEBUG] Identified files to lint in: 7.173542ms
[2024-10-28][10:46:28][ruff::diagnostics][DEBUG] Checking: demos/test.py
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'asyncio' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'enum' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'typing' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'langchain.chains.llm' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'langchain.docstore.document' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'app.generators.utils' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'asyncio' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'enum' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'typing' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'app.generators.utils' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'langchain.chains.llm' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:46:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'langchain.docstore.document' as Known(ThirdParty) (NoMatch)
[2024-10-28][10:46:28][ruff::commands::check][DEBUG] Checked 1 files in: 8.106625ms
Found 1 error (1 fixed, 0 remaining).
Apparently it classifies app.generators.utils as third-party when running as pre-commit. It correctly detects it as first-party via CLI and VS Code
I'll try the following to check where the discrepancy is coming from:
- Run it with
--isolatedflag from both the CLI and pre-commit - Run it with
--no-cacheflag from both the CLI and pre-commit - Both (1) and (2) i.e., use both
--isolatedand--no-cacheflags
Can you try the above and see if it still gives the same results? The --isolated flag will ignore any configuration while the --no-cache will ignore any cache. If it's (1), then there's some configuration that's influencing one and not the other, you might be able to check that with --show-settings flag.