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

feat(config): Add support for path specific config #2527

Closed

Conversation

Shivansh-007
Copy link
Contributor

Lol, I am sorry for spamming 3 PRs. Had completed them before vacation and didn't get time to open them until now :)

Description

Woohoo! Module specific configuration! and a bit of restructuring of code in the black/__init__.py file. Mainly shifting all the "reformatting" logic to a separate function to increase the readability of code in the click CLI function. You can go through the code in the commit, code should be self explanatory, though I still need to update the documentation of the code and this feature.

I haven't aimed at bringing new tests in this commit, if required, I could do that and this rather fixes the tests to work according to the change I have done in this commit.

Summarising the changes:

  • Now, you can support file/module specific configuration in the pyproject.toml config under the section of [tools.black-module]. I have added a separate "key" for the modules as according to the current code when the module name could clash with a config argument. For example [tools.black.src] would clash with SRC.

  • Module specific configuration can only some arguments which are listed under a global constant in config.py and should agree with the type hint present on them in the class variables, the logic for this functionality can be seen in cast_typehint function of config.py. At some places I have ignored MYPY errors so as I couldn't change the configuration since they were being used for typing.

  • Added a new class for storing all the parameters, the order of the arguments is as follows:

    • Read global pyproject.toml/specified config config.
    • Read module specific config, which would take the none specified arguments from the global configuration.
    • If value specified through click options/arguments it would be overwritten, this won't happen if the default value of the option is taken.
    • Tada!

Closes #1370

Checklist - did you ...

  • Add a CHANGELOG entry if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

Woohoo! Module specific configuration! and a bit of restructuring of code in the
`black/__init__.py` file. Mainly shifting all the "reformatting" logic to a
separate function to increase the readability of code in the click CLI
function. You can go through the code in the commit, code should be self
explanatory, though I still need to update the documentation of the
code and this feature.

I haven't aimed at bringing new tests in this commit, if required, I
could do that and this rather fixes the tests to work according to the
change I have done in this commit.

Summarising the changes:

- Now, you can support file/module specific configuration in the
  pyproject.toml config under the section of `[tools.black-module]`. I
  have added a separate "key" for the modules as according to the
  current code when the module name could clash with a config argument.
  For example `[tools.black.src]` would clash with `SRC`.
- Module specific configuration can only some arguments which are listed
  under a global constant in `config.py` and should agree with the
  type hint present on them in the class variables, the logic for this
  functionality can be seen in `cast_typehint` function of `config.py`.
  At some places I have ignored MYPY errors so as I couldn't change the
  configuration since they were being used for typing.
- Added a new class for storing all the parameters, the order of the
  arguments is as follows:
  * Read global pyproject.toml/specified config config.
  * Read module specific config, which would take the none specified
    arguments from the global configuration.
  * If value specified through click options/arguments it would be
    overwritten, this won't happen if the default value of the option is
    taken.
  * Tada!
@felix-hilden
Copy link
Collaborator

Hi and thanks for the submission! But I'll be honest, I'm not sure being able to specify a different value for string normalisation, trailing commas and such in subdirectories is a great idea. I can sort of see it for the documentation line length use case presented in the linked issue, but this really clashes with our consistency and non-configurability ethos. So I'm not sure if practicality beats purity here. Second opinions?

@Shivansh-007
Copy link
Contributor Author

That was one point of discussion I forgot to include in the PR description. I could change the values accordingly once decided.

@ichard26 ichard26 self-requested a review October 7, 2021 15:49
@ichard26
Copy link
Collaborator

ichard26 commented Oct 7, 2021

Hello! I will try to do a more thorough review eventually but I do some initial feedback:

FWIW, it would've been a good idea to get sign off on the original issue before implementing this as this has some ethos design considerations which could easily block it. And for the next contribution I'd encourage putting refactor changes in their own preceding commits so I don't have to spend too much time discerning what changes are for what. Even if code is self-explanatory, that doesn't fix the problem of that it's simply hard to review a big change without clear sections.

Also, what's up with the Pipfile.lock changes? They don't look very correct to me as many dependencies needed on 3.6/7 seem to have been dropped (you can blame pipenv's strong dependence on the runtime version during locking for that 🙃) but I also don't see a reason why Pipfile.lock needs to be touched here. If it's a routine upgrade you're trying to do, I'd suggest moving them to a new PR (although honestly I'd suggest not trying to touch pipenv's lockfiles as it's a shockingly annoying thing to use).

Thanks for the PR though!

@Shivansh-007
Copy link
Contributor Author

Right, I was on an earlier commit while working on this and the pipenv install was failing, showing Pipfile.lock is out of date and some deps were failing to be installed, don't remember the error. So I had to relock the Pipfile. It has been fixed in one of the newer commits, so I will remove that section of the PR.

I do keep my commits atomic for most of my PRs, it's just I saw squashed commits being merged to main. On a few PRs originally people were force pushing their changes to a single commit after addressing the reviews, so I thought it was some sort of a policy to keep the changes squashed.

@ichard26
Copy link
Collaborator

ichard26 commented Oct 8, 2021

I do keep my commits atomic for most of my PRs, it's just I saw squashed commits being merged to main. On a few PRs originally people were force pushing their changes to a single commit after addressing the reviews, so I thought it was some sort of a policy to keep the changes squashed.

We don't really care either way as we prefer squash merges into main regardless of how the branch's history. The key point here is "after addressing the reviews". We prefer to squash during the final merge but reviewing well defined commits (although it's a balancing act between the changes being one big pile vs a giant mess of too many small commits) is so much easier. If anything people shouldn't be force-pushing until we're about to merge but oh well1 🤷.

Footnotes

  1. small PRs are usually exempt from this as it's easy enough to review everything again

@Shivansh-007
Copy link
Contributor Author

Alright, will remember that!

@felix-hilden
Copy link
Collaborator

Just to be clear, my initial gut feeling would be to say no unless we really narrow down the possible configurations. I'd be okay with include, exclude and others that don't change the style. But I'm not sure if it's worth the indirection since we could simply extend the configuration to allow path-specific values, like

exclude = [
    "docs/src/examples",
    "...",
]
line-length = [
    ["docs/src/include", 65],
    ["...", 9000]
]

Or whatever the appropriate TOML syntax is. Although to be fair the regex would be able to handle "multiple values" for exclusion as well 😄 And I'm not saying we should do this either! But if we're going down this route for a narrow subset of configuration values, I think this could be a better way.

@Shivansh-007
Copy link
Contributor Author

Shivansh-007 commented Oct 8, 2021

I like that style to be honest 💯

Though while working on this, I had aimed it to be similar to the mypy configuration style and what had been commented in the issue.

@felix-hilden
Copy link
Collaborator

Yeah, I guess in hindsight it would have been better to discuss further on the issue since it's so old 😅

@ichard26 ichard26 added the C: configuration CLI and configuration label Oct 22, 2021
@JelleZijlstra
Copy link
Collaborator

This has a bunch of merge conflicts now, and there's still no consensus yet on whether we should even do this (but that should be discussed on the issue). Are you still interested in moving this forward?

@Shivansh-007
Copy link
Contributor Author

Yeah, I have been waiting for the design approval, whether we should go with what Felix proposed or the current solution.

@ichard26 ichard26 added the S: blocked A decision or another issue needs to be made/resolved before this can be worked on (more). label Aug 3, 2022
@making787
Copy link

You

@JelleZijlstra
Copy link
Collaborator

Given the discussion on the issue, I'm going to close this for now. Thanks for your contribution!

@Shivansh-007 Shivansh-007 deleted the feat/module-specific-config branch April 2, 2023 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration S: blocked A decision or another issue needs to be made/resolved before this can be worked on (more).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Override line-length for subdirectories
5 participants