From 3bff550710c00cb29c926a464c892eda955e10a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Sun, 12 Apr 2020 12:23:11 +0200 Subject: [PATCH] Allow heterogenous arrays (#92) The 1.0.0 version of the TOML specification allows heterogenous arrays --- tests/conftest.py | 11 +++++++++++ tests/examples/invalid/mixed_array_types.toml | 1 - tests/test_api.py | 1 - tomlkit/items.py | 15 --------------- tomlkit/parser.py | 5 +---- 5 files changed, 12 insertions(+), 21 deletions(-) delete mode 100644 tests/examples/invalid/mixed_array_types.toml diff --git a/tests/conftest.py b/tests/conftest.py index 1b1e8174..14ef54c4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,6 +42,13 @@ def _example(name): TEST_DIR = os.path.join(os.path.dirname(__file__), "toml-test", "tests") +IGNORED_TESTS = { + "invalid": [ + "array-mixed-types-strings-and-ints.toml", + "array-mixed-types-arrays-and-ints.toml", + "array-mixed-types-ints-and-floats.toml", + ] +} def get_tomltest_cases(): @@ -52,8 +59,12 @@ def get_tomltest_cases(): rv = {} for d in dirs: rv[d] = {} + ignored = IGNORED_TESTS.get(d, []) files = os.listdir(os.path.join(TEST_DIR, d)) for f in files: + if f in ignored: + continue + bn, ext = f.rsplit(".", 1) if bn not in rv[d]: rv[d][bn] = {} diff --git a/tests/examples/invalid/mixed_array_types.toml b/tests/examples/invalid/mixed_array_types.toml deleted file mode 100644 index 5cfc8d08..00000000 --- a/tests/examples/invalid/mixed_array_types.toml +++ /dev/null @@ -1 +0,0 @@ -array = [1, "2", 3] diff --git a/tests/test_api.py b/tests/test_api.py index 279f81a2..18919b46 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -77,7 +77,6 @@ def test_parsed_document_are_properly_json_representable( ("section_with_trailing_characters", UnexpectedCharError), ("key_value_with_trailing_chars", UnexpectedCharError), ("array_with_invalid_chars", UnexpectedCharError), - ("mixed_array_types", MixedArrayTypesError), ("invalid_number", InvalidNumberError), ("invalid_date", InvalidDateError), ("invalid_time", InvalidTimeError), diff --git a/tomlkit/items.py b/tomlkit/items.py index 8399d0c3..c073b5e7 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -762,18 +762,6 @@ def discriminant(self): # type: () -> int def value(self): # type: () -> list return self - def is_homogeneous(self): # type: () -> bool - if not self: - return True - - discriminants = [ - i.discriminant - for i in self._value - if not isinstance(i, (Whitespace, Comment)) - ] - - return len(set(discriminants)) == 1 - def multiline(self, multiline): # type: (bool) -> self self._multiline = multiline @@ -801,9 +789,6 @@ def append(self, _item): # type: () -> None self._value.append(it) - if not self.is_homogeneous(): - raise ValueError("Array has mixed types elements") - if not PY2: def clear(self): diff --git a/tomlkit/parser.py b/tomlkit/parser.py index c5cc139c..bd2ad887 100644 --- a/tomlkit/parser.py +++ b/tomlkit/parser.py @@ -674,10 +674,7 @@ def _parse_array(self): # type: () -> Array except ValueError: pass else: - if res.is_homogeneous(): - return res - - raise self.parse_error(MixedArrayTypesError) + return res def _parse_inline_table(self): # type: () -> InlineTable # consume opening bracket, EOF here is an issue (middle of array)