diff --git a/.github/workflows/darker-black-isort.yml b/.github/workflows/darker-black-isort.yml new file mode 100644 index 000000000..f280f9e1f --- /dev/null +++ b/.github/workflows/darker-black-isort.yml @@ -0,0 +1,20 @@ +--- +name: darker-black-isort + +on: push # yamllint disable-line rule:truthy + +jobs: + reformat-modified: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Reformat code using own GitHub action straight from the repository + uses: ./ + with: + options: --check --diff --color --isort + src: action release_tools setup.py src/darker + revision: origin/master... + version: "@${{ github.ref_name }}" diff --git a/.github/workflows/darker-flake8.yml b/.github/workflows/darker-flake8.yml index 9fa3f02de..841ba1954 100644 --- a/.github/workflows/darker-flake8.yml +++ b/.github/workflows/darker-flake8.yml @@ -14,8 +14,8 @@ jobs: - name: Run Darker's own GitHub action straight from the repository uses: ./ with: - options: --check --diff --color --isort --config pyproject.toml - src: src/darker + options: --check --diff --color + src: action release_tools setup.py src/darker revision: origin/master... lint: flake8 version: "@${{ github.ref_name }}" diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index dd42c4718..f267613bf 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -67,6 +67,8 @@ jobs: --run ' python -m venv venv source venv/bin/activate + cert_file=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt + export NIX_SSL_CERT_FILE=$cert_file pip install "${{needs.build-wheel.outputs.wheel-path}}[test]" # Run tests in installed package to avoid plugin import issue: pytest $(python -c " @@ -127,17 +129,7 @@ jobs: if: matrix.constraints == '--constraint constraints-oldest.txt' run: | sed -i 's/py311/py39/' pyproject.toml - - name: Run Pytest with the Darker plugin on recent Black versions - if: matrix.constraints != '--constraint constraints-oldest.txt' - run: | - pytest --darker - - name: Run Pytest without the Darker plugin on oldest Black version - # The reformatting rules used to be a bit different. We don't need to - # test reformatting Darker's own code base with old Black versions. - # Interoperability is ensured by unit tests. - if: matrix.constraints == '--constraint constraints-oldest.txt' - run: | - pytest + - run: pytest build-sdist-validate-dists: runs-on: ubuntu-latest diff --git a/CHANGES.rst b/CHANGES.rst index a61908215..3f21e9e34 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,13 +5,26 @@ These features will be included in the next release: Added ----- +- The command ``darker --config=check-darker.toml`` now runs Flake8_, Mypy_, + pydocstyle_, Pylint_ and Ruff_ on modified lines in Python files. Those tools are + included in the ``[test]`` extra. +- The minimum Ruff_ version is now 0.0.292. Its configuration in ``pyproject.toml`` has + been updated accordingly. +- The contribution guide now gives better instructions for reformatting and linting. +- Separate GitHub workflow for checking code formatting and import sorting. +- Also check the action, release tools and ``setup.py`` in the build workflows. + +Removed +------- +- Don't run pytest-darker_ in the CI build. It's lagging quite a bit behind. Fixed ----- -- Black 24.2.0 compatibility by using the new `darkgraylib.files.find_project_root` +- `Black 24.2.0`_ compatibility by using the new `darkgraylib.files.find_project_root` instead of the implementation in Black. -- Black 24.2.1 compatibility by detecting the new `black.parsing.ASTSafetyError` instead +- `Black 24.2.1`_ compatibility by detecting the new `black.parsing.ASTSafetyError` instead of `AssertionError` when Black>=24.2.1 is in use. +- Make sure NixOS_ builds have good SSL certificates installed. 1.7.3_ - 2024-02-27 @@ -19,13 +32,13 @@ Fixed Added ----- -- Limit Black to versions before 24.2 until the incompatibility is resolved. +- Limit Black_ to versions before 24.2 until the incompatibility is resolved. - Stop testing on Python 3.7. Note: dropping support to be done in a separate PR. Fixed ----- - Typos in README. -- Usage of the Black ``gen_python_files(gitignore_dict=...)`` parameter. +- Usage of the Black_ ``gen_python_files(gitignore_dict=...)`` parameter. - ``show_capture`` option in Pytest configuration. - Ignore some linter messages by recent versions of linters used in CI builds. - Fix compatibility with Pygments 2.4.0 and 2.17.2 in unit tests. @@ -39,8 +52,8 @@ Added ----- - Add a ``News`` link on the PyPI page. - Allow ``-`` as the single source filename when using the ``--stdin-filename`` option. - This makes the option compatible with Black. -- Upgrade NixOS tests to use Python 3.11 on both Linux and macOS. + This makes the option compatible with Black_. +- Upgrade NixOS_ tests to use Python 3.11 on both Linux and macOS. - Move ``git_repo`` fixture to ``darkgraylib``. - In CI builds, show a diff of changed ``--help`` output if ``README.rst`` is outdated. @@ -51,10 +64,10 @@ Fixed - Python 3.12 compatibility in multi-line string scanning. - Python 3.12 compatibility for the GitHub Action. - Use the original repository working directory name as the name of the temporary - directory for getting the linter baseline. This avoids issues with Mypy when there's + directory for getting the linter baseline. This avoids issues with Mypy_ when there's an ``__init__.py`` in the repository root. - Upgrade ``install-nix-action`` to version 22 in CI to fix an issue with macOS. -- Allow ``--target-version=py312`` since newest Black supports it. +- Allow ``--target-version=py312`` since newest Black_ supports it. - Allow a comment in milestone titles in the ``bump_version`` script. @@ -74,8 +87,8 @@ Fixed - In CI builds, run the ``commit-range`` action from the current checkout instead of pointing to a release tag. This fixes workflows when in a release branch. - Linting fixes: Use ``stacklevel=2`` in ``warnings.warn()`` calls as suggested by - Flake8; skip Bandit check for virtualenv creation in the GitHub Action; - use ``ignore[method-assign]`` as suggested by Mypy. + Flake8_; skip Bandit check for virtualenv creation in the GitHub Action; + use ``ignore[method-assign]`` as suggested by Mypy_. - Configuration options spelled with hyphens in ``pyproject.toml`` (e.g. ``line-length = 88``) are now supported. - In debug log output mode, configuration options are now always spelled with hyphens @@ -92,7 +105,7 @@ Added - Make unit tests compatible with ``pytest --log-cli-level==DEBUG``. Doctests are still incompatible due to `pytest#5908 `_. -- Black's ``target-version =`` configuration file option and ``-t`` / +- Black_'s ``target-version =`` configuration file option and ``-t`` / ``--target-version`` command line option - In ``README.rst``, link to GitHub searches which find public repositories that use Darker. @@ -109,7 +122,7 @@ Added Fixed ----- - Compatibility of highlighting unit tests with Pygments 2.14.0. -- In the CI test workflow, don't use environment variables to add a Black version +- In the CI test workflow, don't use environment variables to add a Black_ version constraint to the ``pip`` command. This fixes the Windows builds. - Pass Git errors to stderr correctly both in raw and encoded subprocess output mode. - Add a work-around for cleaning up temporary directories. Needed for Python 3.7 on @@ -119,7 +132,7 @@ Fixed - Configure ``coverage`` to use relative paths in the Darker repository. This enables use of ``cov_to_lint.py`` - Satisfy Pylint's ``use-dict-literal`` check in Darker's code base. -- Use ``!r`` to quote values in format strings as suggested by recent Flake8 versions. +- Use ``!r`` to quote values in format strings as suggested by recent Flake8_ versions. 1.6.1_ - 2022-12-28 @@ -135,7 +148,7 @@ Added Fixed ----- -- Pin Black to version 22.12.0 in the CI build to ensure consistent formatting of +- Pin Black_ to version 22.12.0 in the CI build to ensure consistent formatting of Darker's own code base. - Fix compatibility with ``black-22.10.1.dev19+gffaaf48`` and later – an argument was replaced in ``black.files.gen_python_files()``. @@ -176,7 +189,7 @@ Added - Only run linters, security checks and package builds once in the CI build. - Small simplification: It doesn't matter whether ``isort`` was run or not, only whether changes were made. -- Refactor Black and ``isort`` file exclusions into one data structure. +- Refactor Black_ and ``isort`` file exclusions into one data structure. Fixed ----- @@ -213,7 +226,7 @@ Fixed - Handle files encoded with an encoding other than UTF-8 without an exception. - The GitHub Action now handles missing ``revision:`` correctly. - Update ``cachix/install-nix-action`` to ``v17`` to fix macOS build error. -- Downgrade Python from 3.10 to 3.9 in the macOS NixOS build on GitHub due to a build +- Downgrade Python from 3.10 to 3.9 in the macOS NixOS_ build on GitHub due to a build error with Python 3.10. - Darker now reads its own configuration from the file specified using ``-c``/``--config``, or in case a directory is specified, from ``pyproject.toml`` @@ -235,12 +248,12 @@ Added Fixed ----- -- Fix NixOS builds when ``pytest-darker`` calls ``pylint``. Needed to activate +- Fix NixOS_ builds when ``pytest-darker`` calls ``pylint``. Needed to activate the virtualenv. - Allow more time to pass when checking file modification times in a unit test. Windows tests on GitHub are sometimes really slow. - Multiline strings are now always reformatted completely even if just a part - was modified by the user and reformatted by Black. This prevents the + was modified by the user and reformatted by Black_. This prevents the "back-and-forth indent" symptom. @@ -249,14 +262,14 @@ Fixed Added ----- -- Run tests on CI against Black ``main`` branch to get an early warning of +- Run tests on CI against Black_ ``main`` branch to get an early warning of incompatible changes which would break Darker. - Determine the commit range to check automatically in the GitHub Action. - Improve GitHub Action documentation. - Add Nix CI builds on Linux and macOS. - Add a YAML linting workflow to the Darker repository. -- Updated Mypy to version 0.931. -- Guard against breaking changes in Black by testing against its ``main`` branch +- Updated Mypy_ to version 0.931. +- Guard against breaking changes in Black_ by testing against its ``main`` branch in the ``test-future`` GitHub Workflow. Fixed @@ -279,9 +292,9 @@ Added Fixed ----- - ``regex`` module now always available for unit tests -- Compatibility with NixOS. Keep ``$PATH`` intact so Git can be called. +- Compatibility with NixOS_. Keep ``$PATH`` intact so Git can be called. - Updated tests to pass on new Pygments versions -- Compatibility with Black 22.1 +- Compatibility with `Black 22.1`_ - Removed additional newline at the end of the file with the ``--stdout`` flag compared to without. - Handle isort file skip comment ``#isort:file_skip`` without an exception. @@ -307,13 +320,13 @@ Fixed ----- - ``.py..tmp`` files from VSCode are now correctly compared to corresponding ``.py`` files in earlier revisions of the Git reposiotry -- Honor exclusion patterns from Black configuration when choosing files to reformat. +- Honor exclusion patterns from Black_ configuration when choosing files to reformat. This only applies when recursing directories specified on the command line, and only - affects Black reformatting, not ``isort`` or linters. + affects Black_ reformatting, not ``isort`` or linters. - ``--revision rev1...rev2`` now actually applies reformatting and filters linter output to only lines modified compared to the common ancestor of ``rev1`` and ``rev2`` - Relative paths are now resolved correctly when using the ``--stdout`` option -- Downgrade to Flake8 version 3.x for Pytest compatibility. +- Downgrade to Flake8_ version 3.x for Pytest compatibility. See `tholo/pytest-flake8#81`__ __ https://github.com/tholo/pytest-flake8/issues/81 @@ -327,7 +340,7 @@ Added - Empty and all-whitespace files are now reformatted properly - Darker now allows itself to modify files when called with ``pre-commit -o HEAD``, but also emits a warning about this being an experimental feature -- Mention Black's possible new line range formatting support in README +- Mention Black_'s possible new line range formatting support in README - Darker can now be used in a plain directory tree in addition to Git repositories Fixed @@ -343,16 +356,16 @@ Fixed Added ----- -- Support for Black's ``--skip-magic-trailing-comma`` option +- Support for Black_'s ``--skip-magic-trailing-comma`` option - ``darker --diff`` output is now identical to that of ``black --diff`` - The ``-d`` / ``--stdout`` option outputs the reformatted contents of the single Python file provided on the command line. - Terminate with an error if non-existing files or directories are passed on the command line. This also improves the error from misquoted parameters like ``"--lint pylint"``. - Allow Git test case to run slower when checking file timestamps. CI can be slow. -- Fix compatibility with Black >= 21.7b1.dev9 +- Fix compatibility with Black_ >= 21.7b1.dev9 - Show a simple one-line error instead of full traceback on some unexpected failures -- Skip reformatting files set to be excluded by Black in configuration files +- Skip reformatting files set to be excluded by Black_ in configuration files Fixed ----- @@ -369,7 +382,7 @@ Fixed Added ----- -- Upgrade to and satisfy MyPy 0.910 by adding ``types-toml`` as a test dependency, and +- Upgrade to and satisfy Mypy_ 0.910 by adding ``types-toml`` as a test dependency, and ``types-dataclasses`` as well if running on Python 3.6. - Installation instructions in a Conda environment. @@ -380,8 +393,8 @@ Fixed - AST verification no longer erroneously fails when using ``--isort`` - Historical comparisons like ``darker --diff --revision=v1.0..v1.1`` now actually compare the second revision and not the working tree files on disk. -- Ensure identical Black formatting on Unix and Windows by always passing Unix newlines - to Black +- Ensure identical Black_ formatting on Unix and Windows by always passing Unix newlines + to Black_ 1.2.3_ - 2021-05-02 @@ -399,7 +412,7 @@ Added Fixed ----- -- Compatibility with MyPy 0.812 +- Compatibility with Mypy_ 0.812 - Keep newline character sequence and text encoding intact when modifying files - Installation now works on Windows - Improve compatibility with pre-commit. Fallback to compare against HEAD if @@ -453,7 +466,7 @@ Added requests. In a way, Darker is now eating its own dogfood. - Support commit ranges for ``-r``/``--revision``. Useful for comparing to the best common ancestor, e.g. ``master...``. -- Configure Flake8 verification for Darker's own source code +- Configure Flake8_ verification for Darker's own source code 1.1.0_ - 2020-08-15 @@ -484,7 +497,7 @@ Fixed Added ----- -- Support for black config +- Support for Black_ config - Support for ``-l``/``--line-length`` and ``-S``/``--skip-string-normalization`` - ``--diff`` outputs a diff for each file on standard output - Require ``isort`` >= 5.0.1 and be compatible with it @@ -501,7 +514,7 @@ Added Fixed ----- -- Run `isort` first, and only then do the detailed ``git diff`` for Black +- Run `isort` first, and only then do the detailed ``git diff`` for Black_ 0.1.1_ - 2020-02-17 @@ -550,5 +563,14 @@ Added .. _pytest-darker: https://pypi.org/project/pytest-darker/ .. _Black 19.10: https://github.com/psf/black/blob/master/CHANGES.md#1910b0 .. _Black 20.8: https://github.com/psf/black/blob/master/CHANGES.md#208b0 +.. _Black 22.1: https://github.com/psf/black/blob/main/CHANGES.md#2210 +.. _Black 24.2.0: https://github.com/psf/black/blob/master/CHANGES.md#2420 +.. _Black 24.2.1: https://github.com/psf/black/blob/master/CHANGES.md#2421 .. _Pylint: https://pypi.org/project/pylint .. _pygments: https://pypi.org/project/Pygments/ +.. _Flake8: https://flake8.pycqa.org/ +.. _Mypy: https://www.mypy-lang.org/ +.. _pydocstyle: http://www.pydocstyle.org/ +.. _Ruff: https://astral.sh/ruff +.. _Black: https://black.readthedocs.io/ +.. _NixOS: https://nixos.org/ diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 27581d2dc..4870f60db 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -89,22 +89,32 @@ GitHub Docs. Setting up a development environment ==================================== -To set up an isolated virtualenv for Darker development, run the test suite and lint -the code base on a Unix-like system:: +To set up an isolated virtualenv for Darker development, modify code in your own branch, +run the test suite and lint modified code on a Unix-like system:: git clone git@github.com:akaihola/darker.git python -m venv .venv-darker source .venv-darker/bin/activate cd darker - pip install -e '.[test]' mypy pylint flake8 + pip install -e '.[test]' + git checkout -b my-feature-branch + # modify code + git commit -m "My feature" pytest - pylint src - mypy . - flake8 src - -Before pushing your commits to a feature branch, it's good to run:: - - darker --isort -L mypy -L pylint -L flake8 -r master... . - -This will fix formatting on modified lines and list any linting errors your changes may -have introduced compared to the branching point of your feature branch from ``master``. + darker --config=check-darker.toml + +Darker will fix formatting on modified lines and list any linting errors your changes +may have introduced compared to the branching point of your feature branch from +``master``. + +Darker is configured in ``check-darker.toml`` to do the following on modified lines: +- reformat using Black +- sort imports using isort +- run Flake8 +- run Mypy +- run Pydocstyle +- run Pylint +- run Ruff + +Those tools have also been configured to match the conventions in the Darker code +base. diff --git a/check-darker.toml b/check-darker.toml new file mode 100644 index 000000000..e29e6fad3 --- /dev/null +++ b/check-darker.toml @@ -0,0 +1,33 @@ +# This is the Darker configuration file for running all the code formatters and linters +# on the Darker code base. To use it, run: +# $ darker --config=check-darker.toml + +[tool.black] +# Darker makes Black read its configuration from the file indicated by the `--config` +# option, so we need to mirror the same configuration here and in `pyproject.toml`. +skip-string-normalization = false +target-version = ["py311"] + +[tool.isort] +# Darker makes isort read its configuration from the file indicated by the `--config` +# option, so we need to mirror the same configuration here and in `pyproject.toml`. +profile = "black" +known_first_party = ["darkgraylib", "graylint"] +known_third_party = ["pytest"] + +[tool.darker] +src = [ + "action", + "release_tools", + "src", + "setup.py", +] +revision = "origin/master..." +isort = true +lint = [ + "flake8", + "mypy", + "pydocstyle", + "pylint", + "ruff check", +] diff --git a/pyproject.toml b/pyproject.toml index fc691d28b..cd1bafd80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,18 +3,27 @@ requires = ["setuptools", "wheel"] # PEP 508 specifications. [tool.black] +# Darker makes Black read its configuration from the file indicated by the `--config` +# option, so we need to mirror the same configuration here and in `check-darker.toml`. skip-string-normalization = false target-version = ["py311"] [tool.isort] +# Darker makes isort read its configuration from the file indicated by the `--config` +# option, so we need to mirror the same configuration here and in `check-darker.toml`. profile = "black" known_first_party = ["darkgraylib", "graylint"] known_third_party = ["pytest"] [tool.darker] +# Only minimal options for Darker by default, so it's easier to test while developing. +# To check Darker's own code base with the full set of reformatters and linters, use: +# $ darker --config=check-darker.toml src = [ "action", + "release_tools", "src", + "setup.py", ] revision = "origin/master..." @@ -22,3 +31,24 @@ revision = "origin/master..." # Check import order only with isort. Pylint doesn't support a custom list of first # party packages. We want to consider "darkgraylib" and "graylint" as first party. disable = ["wrong-import-order"] + +[tool.ruff] +target-version = "py38" + +[tool.ruff.lint] +select = ["ALL"] +ignore = [ + "ANN101", # Missing type annotation for `self` in method + "D203", # One blank line required before class docstring + "D213", # Multi-line docstring summary should start at the second line + "D400", # First line should end with a period (duplicates D415) +] + +[tool.ruff.lint.per-file-ignores] +"src/darker/tests/*.py" = [ + "ANN001", # Missing type annotation for function argument + "ANN201", # Missing return type annotation for public function + "ANN204", # Missing return type annotation for special method `__init__` + "C408", # Unnecessary `dict` call (rewrite as a literal) + "S101", # Use of `assert` detected +] diff --git a/setup.cfg b/setup.cfg index 1641d6fbd..2758ec6ee 100644 --- a/setup.cfg +++ b/setup.cfg @@ -60,13 +60,15 @@ test = mypy>=0.990 pathspec # to test `gen_python_files` in `test_black_diff.py` pip-requirements-parser + pydocstyle pygments + pylint pytest>=6.2.0 - pytest-darker pytest-kwparametrize>=0.0.3 regex>=2021.4.4 requests_cache>=0.7 ruamel.yaml>=0.17.21 + ruff>=0.0.292 twine>=2.0.0 types-requests>=2.27.9 types-toml>=0.10.4