ruff: Folder with module name detected as source leads to false `I001` error
This issue affects the expected sorting of imports in Python and hence (incorrectly) triggers a I001 error.
When there is a folder that has the same name as a module, it is (possibly incorrectly) identified as SourceMatch. Ruff then categorizes the module as Known(FirstParty) and adapts the expected sorting accordingly.
This happens commonly, but not exclusively, when using the wandb library because it creates a wandb folder, as discovered in https://github.com/ChartBoost/ruff-action/issues/20.
To reproduce the issue, create a Python file with these imports (I called it test.py):
import csv
import logging
import random
import sys
from typing import Any, Optional, TextIO
import torch
import torch.nn as nn
from torch import optim
from torcheval.metrics import (
Metric,
MulticlassAccuracy,
MulticlassF1Score,
MulticlassPrecision,
MulticlassRecall,
)
from tqdm import tqdm
import wandb
In the initial scenario, there is a wandb directory:
% ls -d wandb/
wandb/
Running Ruff to check the import sorting:
% poetry run ruff check -v --select=I001 test.py
[2024-03-22][08:57:28][ruff::resolve][DEBUG] Using configuration file (via parent) at: /Users/carstenschnober/LAHTeR/workspace/document-segmentation/pyproject.toml
[2024-03-22][08:57:28][ruff::commands::check][DEBUG] Identified files to lint in: 2.013375ms
[2024-03-22][08:57:28][ruff::diagnostics][DEBUG] Checking: /Users/carstenschnober/LAHTeR/workspace/document-segmentation/test.py
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torch.nn' as Known(ThirdParty) (NoMatch)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'csv' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'random' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'sys' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'logging' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torch' as Known(ThirdParty) (NoMatch)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'wandb' as Known(FirstParty) (SourceMatch("/Users/carstenschnober/LAHTeR/workspace/document-segmentation"))
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torch' as Known(ThirdParty) (NoMatch)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torcheval.metrics' as Known(ThirdParty) (NoMatch)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'typing' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][08:57:28][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'tqdm' as Known(ThirdParty) (NoMatch)
[2024-03-22][08:57:28][ruff::commands::check][DEBUG] Checked 1 files in: 765.709µs
All checks passed!
The checks pass, wandb has been categorized as Known(FirstParty)
Now remove the wandb directory:
% mv wandb wandb.bak
% ls -d wandb/
ls: wandb/: No such file or directory
Running the same Ruff check triggers a I001 error on the same file, categorizing wandb as Known(ThirdParty); the module categorization is cached, so I remove the .ruff_cache directory first to reproduce the error:
% rm -r .ruff_cache
% poetry run ruff check -v --select=I001 test.py
[2024-03-22][09:06:42][ruff::resolve][DEBUG] Using configuration file (via parent) at: /Users/carstenschnober/LAHTeR/workspace/document-segmentation/pyproject.toml
[2024-03-22][09:06:42][ruff::commands::check][DEBUG] Identified files to lint in: 1.959083ms
[2024-03-22][09:06:42][ruff::diagnostics][DEBUG] Checking: /Users/carstenschnober/LAHTeR/workspace/document-segmentation/test.py
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torch.nn' as Known(ThirdParty) (NoMatch)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'csv' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'random' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'sys' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'logging' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torch' as Known(ThirdParty) (NoMatch)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'wandb' as Known(FirstParty) (SourceMatch("/Users/carstenschnober/LAHTeR/workspace/document-segmentation"))
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torch' as Known(ThirdParty) (NoMatch)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'torcheval.metrics' as Known(ThirdParty) (NoMatch)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'typing' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-03-22][09:06:42][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'tqdm' as Known(ThirdParty) (NoMatch)
[2024-03-22][09:06:42][ruff::commands::check][DEBUG] Checked 1 files in: 1.004834ms
test.py:1:1: I001 [*] Import block is un-sorted or un-formatted
Found 1 error.
[*] 1 fixable with the `--fix` option.
This is now the expected sorting that is generated when calling ruff --fix call above:
import csv
import logging
import random
import sys
from typing import Any, Optional, TextIO
import torch
import torch.nn as nn
import wandb
from torch import optim
from torcheval.metrics import (
Metric,
MulticlassAccuracy,
MulticlassF1Score,
MulticlassPrecision,
MulticlassRecall,
)
from tqdm import tqdm
This configuration option fixes the issue properly (see https://github.com/ChartBoost/ruff-action/issues/20#issuecomment-2012747916):
[tool.ruff.lint.isort]
known-third-party = ["wandb"]
However, it is difficult for users to identify the issue and fix the configuration accordingly. I think a better solution would be to have a more robust source directory detection.
A heuristics like checking for __init__.py or generally the presence of *.py files as a condition might be a solid starting point for Python.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 9
- Comments: 16 (6 by maintainers)
We should prioritize resolving this issue. There is a known workaround, but it comes up regularly, and reproducing it is a ton of work for users (and us).
I think this is worth considering as sort of a compromise between how Python technically resolves the module and how the user’s would expect (for the purposes of sorting). In theory it may have a performance impact if someone has a heavily nested non-Python folder that shares the name of a package.
Also I’m working on a longer response here to try to explain what’s strange about the relationship between Python’s module resolution and the notion of “first party” in import sorting - hopefully that can help clear some things up… or make everyone more confused, we’ll see!
This cost me half an hour today. I find it particularly surprising that ignored folders influence the categorization.
@dylwil3 do you have any ideas on how we could improve the
wandbcase described here? What I understand is that your most recent change won’t help with this because it will still considerwandbas a possible namespace package (which is correct because Python would do the same)I would echo the same concern, it is really difficult for the end-users to pick up the error.
I had the same issue when using
wandb. I ensured my local and remote CI tests have consistent environments but the differences still persist until I realised that a local folder namewandbis created.Better error message is needed if we can’t have a quick fix easily.
We’re looking into changing how we cache data as part of our multifile analysis work. It will allow us to invalidate caches based on dependencies.
Honestly, I think this should be a separate issue about caching. It is only related in the sense that caching must somehow also handle the detection of third party folders. But if you propose improvements in the caching logic, I think it would make sense to do this separately from this issue (detection of source folders).