Hi all. I’ve got pants set up on my project, but h...
# general
c
Hi all. I’ve got pants set up on my project, but have some questions. If I run
Copy code
./pant fmt ::
multiple times in a row, it reports that changes were made each time. Why is that?
1
b
Your tools might be disagreeing on formatting
c
I’ve got black configured in pyproject.toml, and I thought in that one place.
b

https://media.giphy.com/media/eunDUhLbOz1vEZfFXl/giphy-downsized.gif

🤣 1
What other formatters are you using?
isort
?
c
Yes, sorry, I’ve got isort in there. But I did take steps to get them to “agree”
p
black runs a bit differently in pants. you can use --keep-sandboxes=on_failure for pants to see what is being run. also in pants sandbox, all the source files is passed to black via commandline args, so you’ll need to use something like force-exclude instead of exclude, just to highlight one of the differences.
👀 1
b
c
Yep, did that.
p
to be fair, I haven’t reconciled my isort configuration as well, it produces different results in pants sandbox and outside
b
Any context you can provide would help. What tools are you using? What configs do you have, and where? etc...
c
Here’s the contents of my
pants.toml
file:
Copy code
[GLOBAL]
pants_version = "2.14.0"

backend_packages = [
#    "pants.backend.docker",
#    "pants.backend.docker.lint.hadolint",
    "pants.backend.python",
    "pants.backend.build_files.fmt.black",
    "pants.backend.python.lint.black",
    "pants.backend.python.lint.isort",
    "pants.backend.python.lint.pylint",
    "pants.backend.python.typecheck.mypy",
]


[python]
tailor_pex_binary_targets = false
interpreter_constraints = ["CPython==3.9.*"]

[source]
root_patterns = [
    "project/app",
    "tests",
]

[anonymous-telemetry]
enabled = false

[pylint]
config = "config/.pylintrc"

[test]
output = "all"

[pytest]
extra_requirements.add = []
And here’s what I think is the relevant part of my pyproject.toml file:
Copy code
[tool.isort]
profile = "black"
line_length = 100
skip_glob = "tests"

[tool.black]
line-length = 100
extend-exclude = "tests"

[tool.pylint."MASTER"]
extension-pkg-whitelist=[
    "pydantic",
    "pydantic.typing"
]

[tool.pytest.ini_options]
addopts = [
    "--import-mode=importlib",
]
p
extend-exclude will probably need to be force-exclude
do you know what changes are happening? is it black formatting? import order from isort?
c
Changing to
force-excluded
generated a lot more “Pants cannot infer owners for the following imports…” messages.
Looks like I need to change the order of execution of black and isort. Is there a way to make isort run first and then black?
hmmmm looks like isort and black don’t agree about some import statement formatting.. 😞
I’ll have to pick this up tomorrow, time to pack it in for the day. Thanks for the help!! 🙂
b
Looks like I need to change the order of execution of black and isort. Is there a way to make isort run first and then black?
I think the order in the
backend_packages
controls the order that they run for
fmt
. --- With
isort
specifically, there's https://github.com/pantsbuild/pants/issues/15069 that can result in isort's order changing, but that may not be what you're hitting. Our configuration (that is stable), is: • `pants.toml`: black first, then isort • `pyproject.toml`: (I suspect the
force_...
etc. aren't relevant, but maybe
default_section
is)
Copy code
[tool.black]
line-length = 88
target-version = ['py39']
skip-magic-trailing-comma = true

[tool.isort]
profile = "black"
py_version = 39
line_length = 88
# attempts to do something similar to zimports
force_single_line = true
force_sort_within_sections = true
order_by_type = false
# FIXME: workaround for <https://github.com/pantsbuild/pants/issues/15069>
default_section = "FIRSTPARTY"
--- I've also noticed that a re-run can report changes even if it doesn't actually make any. I suspect this is if the source code is the same as the last run's output and the invocation is somehow cached/not rerun, but am not sure, although this may not be what you're hitting with your follow-up message about
lint
vs
fmt
.
Also, for
extend-exclude
and
skip_glob
, it may be possible to use
__defaults__
(new in 2.14, I believe: https://www.pantsbuild.org/docs/targets#field-default-values) to set
skip_black=True
and
skip_isort=True
for all
python_tests
and
python_test_utils
targets, to make the don't-run-on-tests more pants-friendly
p
not sure why nobody else is running into this issue. I got black to work using force_exclude since pants pass all files as args so if you just do exclude or extend-exclude it would format all the files you excluded because whatever you pass in using cmd line args takes precedence. similar with isort, needed filter_files = true flag in order for skip_blob to take affect
c
@broad-processor-92400 and @plain-night-51324, thanks very much for the feedback, helpful! Here is the relevant section of my pyproject.toml file:
Copy code
[tool.isort]
profile = "black"
py_version = 39
line_length = 100
force_single_line = true
force_sort_within_sections = true
order_by_type = true
default_section = "FIRSTPARTY"
filter_files = true
skip_glob = "tests"

[tool.black]
line-length = 100
target-version = ['py39']
skip-magic-trailing-comma = true
force_exclude = "tests"
Using this
./pants fmt ::
and
./pants lint ::
behave in predictable ways, and don’t seem to “undo” each other. Again, thanks very much for your help and insight!
e
@chilly-tailor-75063 can you share what changed? Previously you all addressed black butting heads with isort but the claim was that was not the issue / you had that sorted IIUC. What did solve it?