Categorized 'setuptools' as Known(FirstParty) (SamePackage)
import setuptools here is an absolute import and should be counted as Known(ThirdParty).
The isort logic appears to be counting this as a first party import in a very specific circumstance. The critical detail here is that ruff_test is a namespace package and has no __init__.py file. If you touch src/ruff_test/__init__.py then you get the expected output Categorized 'setuptools' as Known(ThirdParty) (NoMatch).
Instead of adding the __init__.py it is also possible to define a known-third-party section to solve this issue:
Then you will see Categorized 'setuptools' as Known(ThirdParty) (KnownThirdParty).
If you remove the src declaration from tool.ruff it is still a problem, but I think the src declaration should make it extra clear to ruff that the absolute import of the package we created is ruff_test.setuptools, and not setuptools.
If ruff knew the appropriate top-level package roots then import setuptools would be correctly matched to a third party package because "absolute relative imports" are no longer a thing in Python 3.
Another workaround is setting detect-same-package = false. Another option is also namespace-packages = ["src/ruff_test"].
I opened this issue because I think the fact that I set src = ["src"] should have been enough info for ruff to do the right thing in the original pyproject.toml.
I tested this on ruff 0.3.0 and 0.3.1 and observed the same behavior on both.
repro example
pyproject.toml
example.py
ruff check output
explanation of problem
import setuptools
here is an absolute import and should be counted asKnown(ThirdParty)
.The isort logic appears to be counting this as a first party import in a very specific circumstance. The critical detail here is that
ruff_test
is a namespace package and has no__init__.py
file. If youtouch src/ruff_test/__init__.py
then you get the expected outputCategorized 'setuptools' as Known(ThirdParty) (NoMatch)
.Instead of adding the
__init__.py
it is also possible to define aknown-third-party
section to solve this issue:Then you will see
Categorized 'setuptools' as Known(ThirdParty) (KnownThirdParty)
.If you remove the
src
declaration fromtool.ruff
it is still a problem, but I think thesrc
declaration should make it extra clear to ruff that the absolute import of the package we created isruff_test.setuptools
, and notsetuptools
.If ruff knew the appropriate top-level package roots then
import setuptools
would be correctly matched to a third party package because "absolute relative imports" are no longer a thing in Python 3.Another workaround is setting
detect-same-package = false
. Another option is alsonamespace-packages = ["src/ruff_test"]
.I opened this issue because I think the fact that I set
src = ["src"]
should have been enough info for ruff to do the right thing in the originalpyproject.toml
.I tested this on ruff 0.3.0 and 0.3.1 and observed the same behavior on both.
Possibly related: https://github.com/astral-sh/ruff/issues/5667 https://github.com/astral-sh/ruff/issues/9130