fresh-cat-9082712/23/2021, 5:46 PM
files (for instance, to add some boilerplate code across the monorepo or individual projects, such as disabling a formatter globally and then enabling it on the a project basis). I’ve explored this path and would love to share with the community. To learn more, see the thread.
file for all
targets. I am not considering a manual approach which would only work for a toy project. So we go for a programmatic update of the
files. One path to take was to use an AST / CST (abstract or concrete syntax tree) based approach (since
files are valid Python files) and then just add extra arguments to a function call. This is fairly easy given the low complexity of the
files in my case. A few libraries to consider are: • https://github.com/python-rope/rope • https://github.com/PyCQA/redbaron • https://github.com/berkerpeksag/astor • https://github.com/Instagram/LibCST However, this may mean some work and also not everyone wants to get into the AST business 😄 Another approach was to use an OOTB tool to edit
files which I did. Because
files in Pants share semantics with the
files used in Bazel, I shamelessly used buildozer (part of Bazel build tools) to update my
files. Keep in mind it will reformat the
file with the Bazel style which may be not what you want.
Running these have modified all libraries and tests targets to have those arguments. E.g.
$ buildozer 'set skip_yapf True' 'set skip_mypy True' 'set skip_flake8 True' ///...:%python_tests $ buildozer 'set skip_yapf True' 'set skip_mypy True' 'set skip_flake8 True' ///...:%python_library
was turned into
However, I run Python formatter on the
python_library( skip_flake8 = True, skip_mypy = True, skip_yapf = True, sources = [ "*/**/*.py", "!tests/", ], )
files anyway afterwards, so that wasn’t a problem for me:
There are easy ways to have fine-grained control over what targets you’d like to run the editing command on, but you may need to consult the Bazel docs as they may differ from the ones used in Pants, e.g. editing only a single project files:
python_library( skip_flake8=True, skip_mypy=True, skip_yapf=True, sources=[ "*/**/*.py", "!tests/", ], )
There may be plans to have some functionality similar to what
$ buildozer 'set skip_yapf False' 'set skip_mypy False' projectdir/subdir:%python_library
provides as core of Pants, but until then it may be a sensible approach for some of us. I think it may be worth experimenting with.
curved-television-656812/23/2021, 5:49 PM
Keep in mind it will reformat theThis is easily remedied in a follow up step with:file with the Bazel style which may be not what you want.
hundreds-father-40412/23/2021, 5:50 PM
goal in Pants 2.8+, which gives a generic plugin hook for modifying BUILD files. Right now, it integrates Black and also has some deprecation fixers, like renaming
It could be expanded to do other things like what you're talking about
happy-kitchen-8948212/23/2021, 5:53 PM
busy-vase-3920201/04/2022, 6:03 PM
fresh-cat-9082701/04/2022, 6:24 PM
busy-vase-3920201/04/2022, 6:25 PM
hundreds-father-40401/04/2022, 6:27 PM
busy-vase-3920201/04/2022, 6:30 PM
fresh-cat-9082701/04/2022, 6:35 PM