ruff-pre-commit icon indicating copy to clipboard operation
ruff-pre-commit copied to clipboard

[v0.6.9] I001 error on pre-commit, but not on CLI check

Open pweglik opened this issue 1 year ago • 8 comments

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!

pweglik avatar Oct 16 '24 11:10 pweglik

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.

pweglik avatar Oct 16 '24 11:10 pweglik

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)

MichaReiser avatar Oct 19 '24 19:10 MichaReiser

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.

dhruvmanila avatar Oct 21 '24 04:10 dhruvmanila

@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

pweglik avatar Oct 21 '24 08:10 pweglik

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

pweglik avatar Oct 21 '24 08:10 pweglik

Interesting. Could you try running ruff with ruff check --select I -v file.py and share the logs?

MichaReiser avatar Oct 25 '24 07:10 MichaReiser

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

pweglik avatar Oct 28 '24 09:10 pweglik

I'll try the following to check where the discrepancy is coming from:

  1. Run it with --isolated flag from both the CLI and pre-commit
  2. Run it with --no-cache flag from both the CLI and pre-commit
  3. Both (1) and (2) i.e., use both --isolated and --no-cache flags

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.

dhruvmanila avatar Oct 28 '24 12:10 dhruvmanila