Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for --config in pyproject.toml #1826

Open
grazhopper opened this issue Nov 18, 2020 · 4 comments · May be fixed by #2525
Open

Support for --config in pyproject.toml #1826

grazhopper opened this issue Nov 18, 2020 · 4 comments · May be fixed by #2525
Labels
C: configuration CLI and configuration T: enhancement New feature or request

Comments

@grazhopper
Copy link

I am often dealing with projects that produce multiple libraries from the same repository, which looks a bit like:

src/lib1/pyproject.toml
src/lib1/setup.py
src/lib2/pyproject.toml
src/lib2/setup.py

All libs need to follow the same formatting rules, which means that the black configuration has to be duplicated in each pyproject.toml file.

A possible way to avoid the duplication could be to teach black to handle the already existing --config option when it is provided inside the normal pyproject.toml. Basically something like:

src/lib1/pyproject.toml
src/lib1/setup.py
src/lib2/pyproject.toml
src/lib2/setup.py
src/black.toml

with each pyproject.toml containing:

[tool.black]
config = '../black.toml'

A relative path would be interpreted relative to the folder containing the pyproject.toml file.

Thoughts? Would this be a welcome contribution?

@grazhopper grazhopper added the T: enhancement New feature or request label Nov 18, 2020
@JelleZijlstra JelleZijlstra added the C: configuration CLI and configuration label May 30, 2021
@felix-hilden
Copy link
Collaborator

This seems like a reasonable idea for a solid use case 👌

@ichard26
Copy link
Collaborator

ichard26 commented Jul 8, 2021

I'm happy to support this use case, but I got some questions around the implementation here:

  1. Should we only support POSIX directory separators (/)? - we do this for the regex based options so it would be consistent and not surprising (I'm also not 100% sure how to take a Windows path and convert to pathlib.Path correctly)
  2. Should chaining be supported?
  3. Should user level configuration support this feature? - my guess is that the main reason why someone would use this feature in user config is to change Black's default at looking at pyproject.toml, but for that to work well, changes would have to be made to the project root detection AND this feature's implementation would have to special case this case to work from CWD
  4. Should absolute paths be supported?

I lean towards: 1-yes, 2-maybe, 3-no, and 4-yes.

Also, we might have to add cycle detection to avoid runaway recursion or infinite looping.

@cooperlees
Copy link
Collaborator

cooperlees commented Jul 9, 2021

  1. Should we only support POSIX directory separators (/)? - we do this for the regex based options so it would be consistent and not surprising (I'm also not 100% sure how to take a Windows path and convert to pathlib.Path correctly)

This is exactly what pathlib makes easy. if you use the Path() object everywhere in code pathlib will workout to make a POSIX or Windows based path object. if the string in the config is c:\foo\config.conf on windows it will just work. The '/' overload is treated universally and when you str() or use a method on the respective platform it will "Do the right thing™️". If someone uses c:\config on POSIX, I'm sure an exception will be thrown.

To make pathing work, always do:

config_path = Path(config_dir) / "file"
  • Note the overloaded '/' operator

NOT

config_path = Path(f"{config_dir}/file")

And you should be good. Having Unittests on Ubuntu / Windows will confirm. I did this all for bandersnatch ... and it all works in Winblows.

Shivansh-007 added a commit to Shivansh-007/black that referenced this issue Oct 7, 2021
You can now specify `config` key in pyproject.toml which does the same
job as the CLI option `--config`. The path of config should be relative
to the CWD it is running from. If the file is not found or is invalid
i.e. it is a folder rather a TOML file it would raise
`click.exceptions.FileError`, and if the file can't be parsed it would
raise `tomli.TOMLDecodeError`. All black config keys in the custom
config should be registered under `[black]` and they would overwrite the
config specified in pyproject but not the one specified by `--config`
flag while running black with the CLI.

Closes psf#1826
@Shivansh-007 Shivansh-007 linked a pull request Oct 7, 2021 that will close this issue
3 tasks
Shivansh-007 added a commit to Shivansh-007/black that referenced this issue Oct 7, 2021
You can now specify `config` key in pyproject.toml which does the same
job as the CLI option `--config`. The path of config should be relative
to the CWD it is running from. If the file is not found or is invalid
i.e. it is a folder rather a TOML file it would raise
`click.exceptions.FileError`, and if the file can't be parsed it would
raise `tomli.TOMLDecodeError`. All black config keys in the custom
config should be registered under `[black]` and they would overwrite the
config specified in pyproject but not the one specified by `--config`
flag while running black with the CLI.

Closes psf#1826
Shivansh-007 added a commit to Shivansh-007/black that referenced this issue Jan 14, 2022
You can now specify `config` key in pyproject.toml which does the same
job as the CLI option `--config`. The path of config should be relative
to the CWD it is running from. If the file is not found or is invalid
i.e. it is a folder rather a TOML file it would raise
`click.exceptions.FileError`, and if the file can't be parsed it would
raise `tomli.TOMLDecodeError`. All black config keys in the custom
config should be registered under `[black]` and they would overwrite the
config specified in pyproject but not the one specified by `--config`
flag while running black with the CLI.

Closes psf#1826
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 2, 2024

Note with #4204 we now no longer look at pyproject.toml that is missing a [tool.black] section (we continue to always stop at VCS root). For some layouts, this gives you the ability to effectively inherit configuration from a pyproject.toml in the root of your monorepo.

I'm not sure we should implement this feature without biting the bullet and doing true hierarchical config, as requested in #3952. Given that we now have #4204, how many folks here have a reason to want just this without also #3952?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants