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

Feature Request - add required black version in pyproject.toml #1246

Closed
jvanasco opened this issue Jan 23, 2020 · 2 comments · Fixed by #2300
Closed

Feature Request - add required black version in pyproject.toml #1246

jvanasco opened this issue Jan 23, 2020 · 2 comments · Fixed by #2300
Labels
C: configuration CLI and configuration S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: enhancement New feature or request

Comments

@jvanasco
Copy link

jvanasco commented Jan 23, 2020

Many projects that I contribute to have adopted black. (Congrats and Thank You!)

An issue which has come up in multiple projects is automatic tests failing because of differences in the installed black versions between environments.

For example: a project may indicate a particular version of black in a pre-commit configuration in .pre-commit-config.yaml (https://black.readthedocs.io/en/stable/version_control_integration.html) which will re-format the code to that version... but that will not-necessarily be the same version which is invoked by the local developer or the build/deploy service - as black will consult the pyproject.toml file instead (https://black.readthedocs.io/en/stable/pyproject_toml.html).

This can create issues on automated tests, because the server environment may be configured to install 19.3b0 while the hook or developer's environment is formatting to 19.10b0. This can suggest a developer didn't run black or made an error, when the build/deploy system is what requires adjustment.

A potential solution is to extend the pyproject.toml configuration to have an argument which states the required version:

[tool.black]
line-length = 88
rev = 19.10b0

If specified, black would then raise a fatal exception if invoked with a version that does not match. Perhaps a commandline flag is needed to avoid this for certain causes. Some projects specify a specific version of black for a particular test environment in tox.ini, but nothing is global.

An alternative might also include migrating the version declaration from the .pre-commit-config.yaml into the pyproject.toml file, where it seems to be more applicable. However, I assume the revision was declared in this file for important reasons I do not understand yet.

To summarize:

  • pre-config integration is great, but if a contributor doesn't have pre-commit installed, they'll invoke black directly
  • if a contributor invokes black directly, they may format the code with a different version than the project uses
  • automated systems can pick up a new version of black; by may not pin the version in the tox config for the test. or they use travis.

having a centralized version pin, instead of requiring a version pin for each context, would eliminate many problems.

Thank you.

see also: #420

@Hyperx837
Copy link

Hyperx837 commented Feb 6, 2021

I guess poetry can solve this. try convince your team to use poetry. I don't know any other solutions than this

@JelleZijlstra
Copy link
Collaborator

I agree that this is a useful feature and I'd welcome a PR implementing it. Sorry for the lack of response earlier.

@ichard26 ichard26 added the S: accepted The changes in this design / enhancement issue have been accepted and can be implemented label May 30, 2021
@JelleZijlstra JelleZijlstra added the C: configuration CLI and configuration label May 30, 2021
JelleZijlstra pushed a commit that referenced this issue Jun 3, 2021
Closes #1246: This PR adds a new option (and automatically a toml entry, hooray for existing configuration management 🎉) to require a specific version of Black to be running.

For example: `black --required-version 20.8b -c "format = 'this'"`

Execution fails straight away if it doesn't match `__version__`.
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: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants