Skip to content

Commit

Permalink
format: support black >=24 (#1829)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
airwoodix and koxudaxi committed Feb 1, 2024
1 parent 1320fcb commit 690cb12
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: |
Expand Down
21 changes: 17 additions & 4 deletions datamodel_code_generator/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -131,20 +132,32 @@ 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
warn(
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
Expand Down
5 changes: 5 additions & 0 deletions tests/parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from typing import List, Optional

import black
import pydantic
import pytest
from packaging import version
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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'))

Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_main_kr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from tempfile import TemporaryDirectory

import black
import pytest
from freezegun import freeze_time

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 690cb12

Please sign in to comment.