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

Reproducible black --help output for --python-cell-magics option #3515

Closed
echoix opened this issue Jan 22, 2023 · 2 comments · Fixed by #3516
Closed

Reproducible black --help output for --python-cell-magics option #3515

echoix opened this issue Jan 22, 2023 · 2 comments · Fixed by #3516
Labels
C: configuration CLI and configuration T: bug Something isn't working

Comments

@echoix
Copy link
Contributor

echoix commented Jan 22, 2023

Describe the bug

When calling black --help repeatedly, the output returned is not the same. The description text for option --python-cell-magics includes an enumeration for a list of know python-magics, but the order of the element is always different.

To Reproduce

  1. Execute in a terminal the command black --help.
  2. Execute again in a terminal the command black --help.
  3. Compare the output of both executions.
  4. Repeat indefinitely for no purpose :)

The changes can look like:

(.venv) @echoix ➜ /workspaces/black (main) $ black --help
Usage: black [OPTIONS] SRC ...

  The uncompromising code formatter.

Options:
  -c, --code TEXT                 Format the code passed in as a string.
  -l, --line-length INTEGER       How many characters per line to allow.
                                  [default: 88]
  -t, --target-version [py33|py34|py35|py36|py37|py38|py39|py310|py311]
                                  Python versions that should be supported by
                                  Black's output. [default: per-file auto-
                                  detection]
  --pyi                           Format all input files like typing stubs
                                  regardless of file extension (useful when
                                  piping source on standard input).
  --ipynb                         Format all input files like Jupyter
                                  Notebooks regardless of file extension
                                  (useful when piping source on standard
                                  input).
  --python-cell-magics TEXT       When processing Jupyter Notebooks, add the
                                  given magic to the list of known python-
-                                 magics (python3, capture, pypy, prun,
-                                 python, time, timeit). Useful for formatting
+                                 magics (python, capture, pypy, python3,
+                                 timeit, time, prun). Useful for formatting
                                  cells with custom python magics.
  -x, --skip-source-first-line    Skip the first line of the source code.
  -S, --skip-string-normalization
                                  Don't normalize string quotes or prefixes.
  -C, --skip-magic-trailing-comma
                                  Don't use trailing commas as a reason to
                                  split lines.
  --preview                       Enable potentially disruptive style changes
                                  that may be added to Black's main
                                  functionality in the next major release.
  --check                         Don't write the files back, just return the
                                  status. Return code 0 means nothing would
                                  change. Return code 1 means some files would
                                  be reformatted. Return code 123 means there
                                  was an internal error.
  --diff                          Don't write the files back, just output a
                                  diff for each file on stdout.
  --color / --no-color            Show colored diff. Only applies when
                                  `--diff` is given.
  --fast / --safe                 If --fast given, skip temporary sanity
                                  checks. [default: --safe]
  --required-version TEXT         Require a specific version of Black to be
                                  running (useful for unifying results across
                                  many environments e.g. with a pyproject.toml
                                  file). It can be either a major version
                                  number or an exact version.
  --include TEXT                  A regular expression that matches files and
                                  directories that should be included on
                                  recursive searches. An empty value means all
                                  files are included regardless of the name.
                                  Use forward slashes for directories on all
                                  platforms (Windows, too). Exclusions are
                                  calculated first, inclusions later.
                                  [default: \.pyi?$]
  --exclude TEXT                  A regular expression that matches files and
                                  directories that should be excluded on
                                  recursive searches. An empty value means no
                                  paths are excluded. Use forward slashes for
                                  directories on all platforms (Windows, too).
                                  Exclusions are calculated first, inclusions
                                  later. [default: /(\.direnv|\.eggs|\.git|\.h
                                  g|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.svn
                                  |\.ipynb_checkpoints|_build|buck-
                                  out|build|dist|__pypackages__)/]
  --extend-exclude TEXT           Like --exclude, but adds additional files
                                  and directories on top of the excluded ones.
                                  (Useful if you simply want to add to the
                                  default)
  --force-exclude TEXT            Like --exclude, but files and directories
                                  matching this regex will be excluded even
                                  when they are passed explicitly as
                                  arguments.
  --stdin-filename TEXT           The name of the file when passing it through
                                  stdin. Useful to make sure Black will
                                  respect --force-exclude option on some
                                  editors that rely on using stdin.
  -W, --workers INTEGER RANGE     Number of parallel workers [default: number
                                  of CPUs in the system]  [x>=1]
  -q, --quiet                     Don't emit non-error messages to stderr.
                                  Errors are still emitted; silence those with
                                  2>/dev/null.
  -v, --verbose                   Also emit messages to stderr about files
                                  that were not changed or were ignored due to
                                  exclusion patterns.
  --version                       Show the version and exit.
  --config FILE                   Read configuration from FILE path.
  -h, --help                      Show this message and exit.

Expected behavior

If there is no changes in Black's version, then there shouldn't be changes in the black --help output.

Environment

  • Black's version: main
  • OS and Python version: unchanged clean GitHub Codespaces for this repo, ran the virtualenv instructions in the issue template,
    so, uname -a gives Linux codespaces-391ca7 5.4.0-1098-azure #104~18.04.2-Ubuntu SMP Tue Nov 29 12:13:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux, and it is using Python 3.10.4 (``python -V`)

Additional context

Here comes the "why".
When tools or doc generators keep the output of the help in its files, it leads to always having two lines of diff for the python-cell-magics in a different order, but always the same set. More specifically, in a project like MegaLinter, each build will have changes for black, even when no changes are made to black. Take a look at any PR with a name starting with "[automation] Auto-update linters version, help and documentation" in https://github.com/oxsecurity/megalinter/pulls?&q=is%3Apr+is%3Aclosed+label%3Adependencies+label%3Aautomerge and look at the changed files. A similar problem was reported for Flake8 last year at PyCQA/flake8#1550 and got fixed in PyCQA/flake8#1552. However, for here the changes to solve the problem is as easy as changing one line that was introduced in #2744:
In

black/src/black/__init__.py

Lines 242 to 251 in c5df7b7

@click.option(
"--python-cell-magics",
multiple=True,
help=(
"When processing Jupyter Notebooks, add the given magic to the list"
f" of known python-magics ({', '.join(PYTHON_CELL_MAGICS)})."
" Useful for formatting cells with custom python magics."
),
default=[],
)
,
we can use ', '.join(sorted(PYTHON_CELL_MAGICS)) instead of ', '.join(PYTHON_CELL_MAGICS), for a non-breaking change.

If you accept, I could write a trivial PR for this, but I was writing an issue first!
Thanks for your time!

@echoix echoix added the T: bug Something isn't working label Jan 22, 2023
@ichard26 ichard26 added the C: configuration CLI and configuration label Jan 22, 2023
@ichard26
Copy link
Collaborator

I love the detailed bug report, thanks for going the extra mile kilometre!

You can open a PR, it is a dead-simple fix indeed.

Merci beaucoup!

@echoix
Copy link
Contributor Author

echoix commented Jan 22, 2023

Voilà,

I completed the PR.

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: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants