From a4a3a429871e1e93dce889f02f765a65dfbffb04 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Wed, 14 Feb 2024 03:06:56 +0900 Subject: [PATCH] Fix black module error when 19.10b0 is installed (#1855) * Fix black module error * Add unittest pattern * skip unsupported unittest * skip unsupported unittest --- .github/workflows/test.yml | 6 ++ datamodel_code_generator/format.py | 6 +- tests/test_main.py | 92 ++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 931872210..6ca46e754 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,11 @@ jobs: black-version: default python-version: 3.8 pydantic-version: 1.8.2 + - os: ubuntu-latest + isort-version: 5.6.4 + black-version: 19.10b0 + python-version: 3.9 + pydantic-version: 1.8.2 - os: ubuntu-latest isort-version: 5.6.4 black-version: 24.1.0 @@ -31,6 +36,7 @@ jobs: black-version: 23.12.1 python-version: 3.12 pydantic-version: 2.4.2 + exclude: - os: windows-latest black-version: 22.1.0 diff --git a/datamodel_code_generator/format.py b/datamodel_code_generator/format.py index f5a25b421..7eb4a3459 100644 --- a/datamodel_code_generator/format.py +++ b/datamodel_code_generator/format.py @@ -7,11 +7,15 @@ from warnings import warn import black -import black.mode import isort from datamodel_code_generator.util import cached_property, load_toml +try: + import black.mode +except ImportError: # pragma: no cover + black.mode = None + class PythonVersion(Enum): PY_36 = '3.6' diff --git a/tests/test_main.py b/tests/test_main.py index d73c9ab35..dfdd2f628 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -622,6 +622,10 @@ def test_main_no_file(capsys: CaptureFixture) -> None: ), ], ) +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_extra_template_data_config( capsys: CaptureFixture, output_model, expected_output ) -> None: @@ -964,6 +968,10 @@ def test_main_without_field_constraints(output_model, expected_output): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_with_aliases(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -1135,6 +1143,10 @@ def test_enable_version_header(): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_allow_population_by_field_name(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -1170,6 +1182,10 @@ def test_allow_population_by_field_name(output_model, expected_output): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_allow_extra_fields(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -1205,6 +1221,10 @@ def test_allow_extra_fields(output_model, expected_output): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_enable_faux_immutability(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -2823,6 +2843,10 @@ def test_main_openapi_nullable_strict_nullable(): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_openapi_pattern(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -4121,6 +4145,10 @@ def test_long_description(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_long_description_wrap_string_literal(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -4311,6 +4339,10 @@ def test_jsonschema_without_titles_use_title_as_name(): ), ], ) +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_use_annotated_with_field_constraints(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -5182,6 +5214,10 @@ def test_main_jsonschema_pattern_properties_by_reference(): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_openapi_default_object(output_model, expected_output): with TemporaryDirectory() as output_dir: output_path: Path = Path(output_dir) @@ -5277,6 +5313,10 @@ def test_main_dataclass_field(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_jsonschema_enum_root_literal(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -5968,6 +6008,10 @@ def test_main_jsonschema_discriminator_literals(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_openapi_all_of_with_relative_ref(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6023,6 +6067,10 @@ def test_main_msgspec_struct(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_msgspec_use_annotated_with_field_constraints(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6092,6 +6140,10 @@ def test_main_duplicate_field_constraints(): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_duplicate_field_constraints_msgspec( collapse_root_models, python_version, expected_output ): @@ -6190,6 +6242,10 @@ def test_main_all_of_ref_self(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_array_field_constraints(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6252,6 +6308,10 @@ def test_all_of_use_default(): ], ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_simple_star_wars(output_model, expected_output): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6275,6 +6335,10 @@ def test_main_graphql_simple_star_wars(output_model, expected_output): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_different_types_of_fields(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6300,6 +6364,10 @@ def test_main_graphql_different_types_of_fields(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_custom_scalar_types(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6325,6 +6393,10 @@ def test_main_graphql_custom_scalar_types(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_field_aliases(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6350,6 +6422,10 @@ def test_main_graphql_field_aliases(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_enums(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6371,6 +6447,10 @@ def test_main_graphql_enums(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_union(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6429,6 +6509,10 @@ def test_main_graphql_additional_imports_isort_4(): reason='See https://github.com/PyCQA/isort/issues/1600 for example', ) @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_additional_imports_isort_5(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6458,6 +6542,10 @@ def test_main_graphql_additional_imports_isort_5(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_graphql_custom_formatters(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py' @@ -6483,6 +6571,10 @@ def test_main_graphql_custom_formatters(): @freeze_time('2019-07-26') +@pytest.mark.skipif( + black.__version__.split('.')[0] == '19', + reason="Installed black doesn't support the old style", +) def test_main_openapi_discriminator_enum(): with TemporaryDirectory() as output_dir: output_file: Path = Path(output_dir) / 'output.py'