Hey, I'm having a strange issue with isort. When I...
# general
c
Hey, I'm having a strange issue with isort. When I run
pants lint ::
it fails claiming isort would make changes. So I run
pants fmt ::
and isort indeed does make changes. Then I run
lint
again, and it wants to revert the changes it made. This issue appeared after I upgraded to Pants 2.15 from an earlier version. Am I doing something wrong? I have seen https://www.pantsbuild.org/v2.17/docs/python-linters-and-formatters#isort-possible-issues-with-its-import-classifier-algorithm, and based on this I updated my
[isort]
section in
pants.toml
by adding the appropriate args, but it didn't help. Thanks!
h
Ugh, this again...
Sorry for the trouble, isort is finicky when run on slices of the repo
Would you be able to publish a simple repo that reproduces the problem without exposing your proprietary code? That would require some debugging to figure out what exactly the issue is.
What are some examples of the changes it makes and then reverts?
c
Hi @happy-kitchen-89482, sorry for such a late reply, I missed your comments! But I'm still having this issue. I had to switch isort off for the whole codebase which is vey much not ideal. I tried to create a toy repo to reproduce the issue, but I'm somehow unable to. Let me show you what's wrong with my codebase on the example of one python file, which is inside
my_module_A
. The imports are as follows:
Copy code
import os

import pandas as pd
from my_module_B import stuff

from my_module_A import other_stuff
I'm indifferent to treating
my_module_B
as an external lib (like pandas) or internal (like
my_module_A
). Running lint makes isort fail on this file. So I call fmt. No changes made in this file. Lint still says it's wrong. Fmt still makes no chages. So I run isort without pants. It changes the imports as follows:
Copy code
import os

import pandas as pd

from my_module_A import other_stuff
from my_module_B import stuff
I'm fine with this, too. I run lint -- it fails. Run fmt -- it changes to the initial imports layout. Run lint again - still fails.
h
Yeeesh. Sorry for the trouble. can you open an issue for this?
A toy reproduction would be still gold, if you can somehow create one
For example, it may be the presence/content of
__init__.py
files that affects this
b
Hey @happy-kitchen-89482: is there any update on this? I'm running into exactly the same issue.
pants fmt ::
does nothing, but in
pants lint ::
isort complains 😕
It's even worse: if I ran pants lint path/to/failing/file.py, it freaking works! 😂
h
Hi @brash-glass-61350, sorry for the trouble. This is an ongoing issue with how isort detects first-party vs third-party packages. Have you tried the advice here ?
If you can post a small public github repo that demonstrates the problem, we can debug on that
Without a repro this is hard to get into the details of
b
Hi @happy-kitchen-89482: I tried adding
known_first_party = ["lib"]
to
pants.toml
, but I just got an error message saying unknown field or something like that. I am supposed to put it somewhere else?
h
This is isort config, not Pants config, so it goes in pyproject.toml (as mentioned in the link above)
b
Ahh, got it! It seems to fix the problem. At least for now. Thanks! 🙏
🎉 1
r
I’m encountering this same problem. The appears that the
known_first_party
configuration fixes the problem, but that’s not a reasonable long-term solution. That would mean that every time we add another project to the root, which we do often, then we would have to update this configuration. Is there a way to pass this configuration through the command line? I see here that it is possible with
-p/--project
in
isort
, but I’m not sure how to pass that through
pants
. If this is possible, then we could use a command to dynamically create this list before passing it to
isort
. Alternatively, I guess we could write a plugin to dynamically write
.isort.cfg
. In that case, is it possible to alias
fmt
and
lint
such that another plug-in runs before
fmt
and
lint
?
h
Yeah, this was discussed- Pants knows which packages are first-party, so we could generate that into isort config
I think there's a ticket for it
It just need someone to implement it, if you're interested?
r
Sounds like @bitter-ability-32190 has the fix on a branch?
n
So it seems Josh's fix is not performant. Is there a one off command to list all first party imports for adding to isort config?