Hello, we’re integrating black with pants in a dja...
# general
q
Hello, we’re integrating black with pants in a django project and we want all the migrations file to be skipped from black… we already have in our
pyproject.toml
the following:
Copy code
exclude = '''
/(
  | \.git
  | \.mypy_cache
  | \.venv
  | migrations
  | protos
)/
'''
but running
./pants lint ::
it does not skip them (while running
black .
it does). We also tried writing the following in the root BUILD file:
Copy code
python_sources(
    name="migrations",
    skip_black=True,
    sources=["**/migrations/*.py"]
)
but it didn’t work. We know we can put the
skip_black=True
in each of the
BUILD
files inside all
migrations
folders but this would require us to add it every time we have a new
migrations
folder, which is a bit of a pain. Can you help us?
1
🙏 1
b
The issue, I think, is Black doesn't respect
exclude
if you provide filenames to the cmd. I think they added a
force-exclude
option for this?
🙌 1
s
if you’re using Pants >= 2.14, you could use
__defaults__
to set
skip_black=True
for all of your migrations
something like:
Copy code
# In migrations/BUILD.pants
__defaults__(
  {(python_sources, python_source): dict(skip_black=True)}
)
q
@sparse-lifeguard-95737 The fact is we have a lot of folders containing a
migrations
sub_folder
s
ah, hrm
q
@bitter-ability-32190 thanks for the hint we’ll try
@bitter-ability-32190 it worked 🎉 thanks a lot
b
FWIW you'll be doing some wasted work here, invoking Pants to run black and black then no-ops, but 🤷‍♂️
q
true that, do you know if there is a way to write some defaults in the root build file to skip them?
b
Hmmmmmmmmm
No, and I'm thinking of th elowest-bar way of doing it and I got nothing easy
q
ok thanks
c
I think you could get away with excludes and pants aliases..
Copy code
# pants.toml
[cli.alias]
exclude-migrations = "!src/apps/foo/migrations:: !src/apps/bar/migrations::"
Then when formatting:
Copy code
./pants fmt exclude-migrations src/apps::
h
What you had could work;
Copy code
python_sources(
    name="migrations",
    skip_black=True,
    sources=["**/migrations/*.py"]
)
but I’m guessing that you also have another
python_sources
that also globs over the migrations?
q
We also have some BUILD files created by
./pants tailor
in all of the
migrations
folders and all of them contains a
python_sources()
so I guess they all override it, am I correct?
@happy-kitchen-89482 We just needed to remove all
**/migrations/BUILD
or am I taking a wrong conclusion?
1
h
That would work, yes. And since that higher-level
migrations
target would cover new migration dirs as well, tailor wouldn’t generate new BUILD files for those new migrations
🔝 1
q
Great thanks a lot