diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a597e797..d8b51ab8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -47,7 +47,6 @@ repos: [ types-PyYAML, types-requests, - types-toml, ] verbose: true diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bc13530a..fb34739a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -56,6 +56,10 @@ Changed - The ``yaml`` dump format now writes multi-line strings as literal blocks, i.e. ``|``, instead of escaping the line breaks (`#972 `__). +- The ``toml`` extra now installs the maintained ``tomli-w`` for writing and + ``tomli`` for reading in python 3.10, instead of the unmaintained ``toml`` + package. Dumped toml arrays are now multi-line (`#973 + `__). Removed ^^^^^^^ diff --git a/jsonargparse/_optionals.py b/jsonargparse/_optionals.py index 40ab9a67..ee399e2f 100644 --- a/jsonargparse/_optionals.py +++ b/jsonargparse/_optionals.py @@ -11,8 +11,8 @@ from typing import Any, Union pyyaml_available = bool(find_spec("yaml")) -toml_load_available = bool(find_spec("toml") or find_spec("tomllib")) -toml_dump_available = bool(find_spec("toml")) +toml_load_available = bool(find_spec("tomllib") or find_spec("tomli")) +toml_dump_available = bool(find_spec("tomli_w")) typing_extensions_support = find_spec("typing_extensions") is not None typeshed_client_support = find_spec("typeshed_client") is not None jsonschema_support = find_spec("jsonschema") is not None @@ -113,16 +113,16 @@ def import_toml_loads(importer): return tomllib.loads, tomllib.TOMLDecodeError else: - with missing_package_raise("toml", importer): - import toml + with missing_package_raise("tomli", importer): + import tomli - return toml.loads, toml.TomlDecodeError + return tomli.loads, tomli.TOMLDecodeError def import_toml_dumps(importer): - with missing_package_raise("toml", importer): - import toml - return toml.dumps + with missing_package_raise("tomli-w", importer): + import tomli_w + return tomli_w.dumps def import_jsonschema(importer): diff --git a/jsonargparse_tests/test_loaders_dumpers.py b/jsonargparse_tests/test_loaders_dumpers.py index 950fbd83..47d7f21c 100644 --- a/jsonargparse_tests/test_loaders_dumpers.py +++ b/jsonargparse_tests/test_loaders_dumpers.py @@ -194,11 +194,14 @@ def test_nested_parser_mode(parser): [group] child1 = 1.2 -child2 = [ 3.0, 4.5,] +child2 = [ + 3.0, + 4.5, +] """ -@pytest.mark.skipif(not toml_load_available, reason="tomllib or toml package is required") +@pytest.mark.skipif(not toml_load_available, reason="tomllib or tomli package is required") def test_toml_parse_args_config(parser, tmp_cwd): parser.parser_mode = "toml" config_path = Path("config.toml") @@ -212,7 +215,7 @@ def test_toml_parse_args_config(parser, tmp_cwd): assert cfg.group.as_dict() == {"child1": 1.2, "child2": [3.0, 4.5]} -@pytest.mark.skipif(not toml_dump_available, reason="toml package is required") +@pytest.mark.skipif(not toml_dump_available, reason="tomli-w package is required") def test_toml_print_config(parser): parser.parser_mode = "toml" parser.add_argument("--config", action="config") diff --git a/pyproject.toml b/pyproject.toml index cb38d3b0..406c168d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,8 @@ yaml = [ "PyYAML>=3.13", ] toml = [ - "toml>=0.10.2", + "tomli>=2.0.1; python_version < '3.11'", + "tomli-w>=1.0.0", ] jsonnet = [ "jsonnet>=0.21.0",