Repository: psf/black

Issue:
Reproducible `black --help` output for `--python-cell-magics` option
<!--
Please make sure that the bug is not already fixed either in newer versions or the
current development version. To confirm this, you have three options:

1. Update Black's version if a newer release exists: `pip install -U black`
2. Use the online formatter at <https://black.vercel.app/?version=main>, which will use
   the latest main branch.
3. Or run _Black_ on your machine:
   - create a new virtualenv (make sure it's the same Python version);
   - clone this repository;
   - run `pip install -e .[d]`;
   - run `pip install -r test_requirements.txt`
   - make sure it's sane by running `python -m pytest`; and
   - run `black` like you did last time.
-->

**Describe the bug**

<!-- A clear and concise description of what the bug is. -->

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:
```diff
(.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**

<!-- A clear and concise description of what you expected to happen. -->
If there is no changes in Black's version, then there shouldn't be changes in the `black --help` output.

**Environment**

<!-- Please complete the following information: -->

- Black's version: main <!-- e.g. [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`) <!-- e.g. [Linux/Python 3.7.4rc1] -->

**Additional context**

<!-- Add any other context about the problem here. -->

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 https://github.com/PyCQA/flake8/issues/1550 and got fixed in https://github.com/PyCQA/flake8/issues/1552. However, for here the changes to solve the problem is as easy as changing one line that was introduced in https://github.com/psf/black/pull/2744:
In https://github.com/psf/black/blob/c5df7b7d3ca5b568a81a9ccc1f647c183e8b4075/src/black/__init__.py#L242-L251,
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!






Discussion:
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!

Resolving patch:
diff --git a/src/black/__init__.py b/src/black/__init__.py
--- a/src/black/__init__.py
+++ b/src/black/__init__.py
@@ -244,7 +244,7 @@ def validate_regex(
     multiple=True,
     help=(
         "When processing Jupyter Notebooks, add the given magic to the list"
-        f" of known python-magics ({', '.join(PYTHON_CELL_MAGICS)})."
+        f" of known python-magics ({', '.join(sorted(PYTHON_CELL_MAGICS))})."
         " Useful for formatting cells with custom python magics."
     ),
     default=[],


Test patch: