From 690cb1281da0f22dba07dacf4ad1b3d956531ba7 Mon Sep 17 00:00:00 2001 From: Etienne Wodey <44871469+airwoodix@users.noreply.github.com> Date: Thu, 1 Feb 2024 19:07:27 +0100 Subject: [PATCH] format: support black >=24 (#1829) * format: support black >=24 Fixes #1821 * format: refine string_processing detection for black >= 24 * Add test pattern * Ignore unsupported tests --------- Co-authored-by: Koudai Aono --- .github/workflows/test.yml | 16 +++++++++++++--- datamodel_code_generator/format.py | 21 +++++++++++++++++---- tests/parser/test_openapi.py | 5 +++++ tests/test_main.py | 28 ++++++++++++++++++++++++++++ tests/test_main_kr.py | 5 +++++ 5 files changed, 68 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 717a343e1..931872210 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,16 @@ jobs: black-version: default python-version: 3.8 pydantic-version: 1.8.2 + - os: ubuntu-latest + isort-version: 5.6.4 + black-version: 24.1.0 + python-version: 3.12 + pydantic-version: 2.4.2 + - os: ubuntu-latest + isort-version: 5.6.4 + black-version: 23.12.1 + python-version: 3.12 + pydantic-version: 2.4.2 exclude: - os: windows-latest black-version: 22.1.0 @@ -91,10 +101,10 @@ jobs: if: matrix.pydantic-version != 'default' run: | poetry run pip install pydantic=="${{ matrix.pydantic-version }}" - - name: Install Black 22.1.0 - if: matrix.black-version == '22.1.0' + - name: Install Black ${{ matrix.black-version }} + if: matrix.black-version != 'default' run: | - poetry run pip install black=="22.1.0" + poetry run pip install black=="${{ matrix.black-version }}" - name: Lint if: matrix.pydantic-version == 'default' run: | diff --git a/datamodel_code_generator/format.py b/datamodel_code_generator/format.py index fd92b8269..f5a25b421 100644 --- a/datamodel_code_generator/format.py +++ b/datamodel_code_generator/format.py @@ -7,6 +7,7 @@ from warnings import warn import black +import black.mode import isort from datamodel_code_generator.util import cached_property, load_toml @@ -131,9 +132,15 @@ def __init__( if wrap_string_literal is not None: experimental_string_processing = wrap_string_literal else: - experimental_string_processing = config.get( - 'experimental-string-processing' - ) + if black.__version__ < '24.1.0': # type: ignore + experimental_string_processing = config.get( + 'experimental-string-processing' + ) + else: + experimental_string_processing = config.get('preview', False) and ( + config.get('unstable', False) + or 'string_processing' in config.get('enable-unstable-feature', []) + ) if experimental_string_processing is not None: # pragma: no cover if black.__version__.startswith('19.'): # type: ignore @@ -141,10 +148,16 @@ def __init__( f"black doesn't support `experimental-string-processing` option" # type: ignore f' for wrapping string literal in {black.__version__}' ) - else: + elif black.__version__ < '24.1.0': # type: ignore black_kwargs[ 'experimental_string_processing' ] = experimental_string_processing + elif experimental_string_processing: + black_kwargs['preview'] = True + black_kwargs['unstable'] = config.get('unstable', False) + black_kwargs['enabled_features'] = { + black.mode.Preview.string_processing + } if TYPE_CHECKING: self.black_mode: black.FileMode diff --git a/tests/parser/test_openapi.py b/tests/parser/test_openapi.py index 605f1d99d..cb5b87369 100644 --- a/tests/parser/test_openapi.py +++ b/tests/parser/test_openapi.py @@ -2,6 +2,7 @@ from pathlib import Path from typing import List, Optional +import black import pydantic import pytest from packaging import version @@ -713,6 +714,10 @@ def test_openapi_parser_responses_with_tag(): ) +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) def test_openapi_parser_with_query_parameters(): parser = OpenAPIParser( data_model_field_type=DataModelFieldBase, diff --git a/tests/test_main.py b/tests/test_main.py index ab0edfc7d..20d7f6340 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -703,6 +703,10 @@ def test_main_custom_template_dir(capsys: CaptureFixture) -> None: assert captured.err == inferred_message.format('openapi') + '\n' +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @freeze_time('2019-07-26') def test_pyproject(): if platform.system() == 'Windows': @@ -1755,6 +1759,10 @@ def test_main_use_standard_collections(tmpdir_factory: TempdirFactory) -> None: assert result == path.read_text() +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) def test_main_use_generic_container_types(tmpdir_factory: TempdirFactory) -> None: output_directory = Path(tmpdir_factory.mktemp('output')) @@ -1781,6 +1789,10 @@ def test_main_use_generic_container_types(tmpdir_factory: TempdirFactory) -> Non assert result == path.read_text() +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @pytest.mark.benchmark def test_main_use_generic_container_types_standard_collections( tmpdir_factory: TempdirFactory, @@ -2366,6 +2378,10 @@ def test_main_openapi_use_one_literal_as_default(): version.parse(pydantic.VERSION) < version.parse('1.9.0'), reason='Require Pydantic version 1.9.0 or later ', ) +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @freeze_time('2019-07-26') def test_main_openapi_enum_models_as_literal_all(): with TemporaryDirectory() as output_dir: @@ -2397,6 +2413,10 @@ def test_main_openapi_enum_models_as_literal_all(): version.parse(pydantic.VERSION) < version.parse('1.9.0'), reason='Require Pydantic version 1.9.0 or later ', ) +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @freeze_time('2019-07-26') def test_main_openapi_enum_models_as_literal_py37(capsys): with TemporaryDirectory() as output_dir: @@ -2687,6 +2707,10 @@ def test_main_all_of_with_object(): ) +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @freeze_time('2019-07-26') def test_main_combined_array(): with TemporaryDirectory() as output_dir: @@ -3359,6 +3383,10 @@ def test_main_strict_types(): ) +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @freeze_time('2019-07-26') def test_main_strict_types_all(): with TemporaryDirectory() as output_dir: diff --git a/tests/test_main_kr.py b/tests/test_main_kr.py index 744c65be2..13e157f04 100644 --- a/tests/test_main_kr.py +++ b/tests/test_main_kr.py @@ -3,6 +3,7 @@ from pathlib import Path from tempfile import TemporaryDirectory +import black import pytest from freezegun import freeze_time @@ -180,6 +181,10 @@ def test_main_custom_template_dir(capsys: CaptureFixture) -> None: assert captured.err == inferred_message.format('openapi') + '\n' +@pytest.mark.skipif( + black.__version__.split('.')[0] >= '24', + reason="Installed black doesn't support the old style", +) @freeze_time('2019-07-26') def test_pyproject(): with TemporaryDirectory() as output_dir: