Python tests: use black and isort for formatting #24759

issue KevinMusgrave opened this issue on April 4, 2022
  1. KevinMusgrave commented at 1:30 PM on April 4, 2022: contributor

    There doesn't seem to be an agreed upon code formatter for the files in tests/.

    Some widely used formatters are black, which does general code formatting, and isort, which sorts the imports. Example usage:

    black .
    isort . --profile black
    

    This could reduce time spent reviewing and discussing code style.

  2. KevinMusgrave added the label Feature on Apr 4, 2022
  3. MarcoFalke commented at 1:41 PM on April 4, 2022: member

    Generally, if the style is not mentioned nor enforced by the developer notes, we leave it up to the original author to pick whatever fits them best personally and then leave it that way until the line is touched for other reasons.

    Enforcing the code style is, as you mentioned not agreed upon. One issue would be that it forces every contributor to use a dev environment that formats the code. Another one is that our current tests reformatted with black don't look great, as black enforces short lines. (Fixing this would require large-scale refactoring of most of the tests)

    I don't think there is any issue to be fixed here. As mentioned previously, we leave the style up to the original author. Thus, no time is wasted on style during review. Of course, anyone is free to use black, if they like for their own code they submit.

  4. MarcoFalke closed this on Apr 4, 2022

  5. MarcoFalke commented at 2:21 PM on April 4, 2022: member

    Maybe it could make sense to mention black in our docs?

    diff --git a/doc/productivity.md b/doc/productivity.md
    index a01c6f545d..c05310d5b5 100644
    --- a/doc/productivity.md
    +++ b/doc/productivity.md
    @@ -123,7 +123,13 @@ Writing code
     
     See [contrib/devtools/README.md](/contrib/devtools/README.md#clang-format-diff.py).
     
    -### Format Python diffs with `yapf-diff.py`
    +### Format Python
    +
    +#### Format Python files
    +
    +For newly created test files, contributors are free to (but not required to) format the whole file with the Python formatter `black`.
    +
    +#### Format Python diffs with `yapf-diff.py`
     
     Usage is exactly the same as [`clang-format-diff.py`](#format-cc-diffs-with-clang-format-diffpy). You can get it [here](https://github.com/MarcoFalke/yapf-diff).
     
    
  6. KevinMusgrave commented at 3:26 PM on April 4, 2022: contributor

    isort might be good to mention too. Since it just sorts imports, I think it's pretty harmless. If applied to existing test files, the diff probably wouldn't be very big. Also it might reduce the number of times reviewers have to bring it up, like here. I don't know if that happens very often though.

  7. jonatack commented at 8:22 AM on April 5, 2022: member

    our current tests reformatted with black don't look great, as black enforces short lines. (Fixing this would require large-scale refactoring of most of the tests)

    Agree with @MarcoFalke about black. I use it sometimes as a check but it generally makes too many changes to use as-is.

  8. DrahtBot locked this on Apr 5, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-01 12:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me