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

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

Open
pweglik opened this issue Oct 16, 2024 · 8 comments
Open

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

pweglik opened this issue Oct 16, 2024 · 8 comments

Comments

@pweglik
Copy link

pweglik commented Oct 16, 2024

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
Copy link
Author

pweglik commented Oct 16, 2024

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.

@MichaReiser
Copy link
Member

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)

@dhruvmanila
Copy link
Member

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.

@pweglik
Copy link
Author

pweglik commented Oct 21, 2024

@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
Copy link
Author

pweglik commented Oct 21, 2024

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

@MichaReiser
Copy link
Member

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

@pweglik
Copy link
Author

pweglik commented Oct 28, 2024

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

@dhruvmanila
Copy link
Member

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.

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