Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to comply with TOML spec v1.0 #154

Merged
merged 6 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Drop support for Python<3.6. ([#151](https://github.com/sdispater/tomlkit/pull/151))
- Comply with TOML v1.0.0. ([#154](https://github.com/sdispater/tomlkit/pull/154))

### Fixed

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# TOML Kit - Style-preserving TOML library for Python

TOML Kit is a **1.0.0rc1-compliant** [TOML](https://github.com/toml-lang/toml) library.
TOML Kit is a **1.0.0-compliant** [TOML](https://toml.io/) library.

It includes a parser that preserves all comments, indentations, whitespace and internal element ordering,
and makes them accessible and editable via an intuitive API.
Expand Down Expand Up @@ -166,6 +166,7 @@ If not, you can use `pip`:
```bash
pip install tomlkit
```

## Running tests

Please clone the repo with submodules with the following command
Expand Down
83 changes: 82 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pytest = "^6.2.5"
pytest-cov = "^3.0.0"
PyYAML = "^6.0"
pre-commit = {version = "^2.1.0", python = "^3.6.1"}
mypy = "^0.920"

[tool.black]
line-length = 88
Expand Down
46 changes: 28 additions & 18 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ 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",
"valid": [
"float/inf-and-nan", # Can't compare nan
]
}

Expand All @@ -55,21 +53,33 @@ def get_tomltest_cases():
dirs = sorted(
f for f in os.listdir(TEST_DIR) if os.path.isdir(os.path.join(TEST_DIR, f))
)
assert dirs == ["invalid", "invalid-encoder", "valid"]
rv = {}
assert dirs == ["invalid", "valid"]
rv = {"invalid_encode": {}}
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] = {}
with open(os.path.join(TEST_DIR, d, f), encoding="utf-8") as inp:
rv[d][bn][ext] = inp.read()

for root, _, files in os.walk(os.path.join(TEST_DIR, d)):
relpath = os.path.relpath(root, os.path.join(TEST_DIR, d))
if relpath == ".":
relpath = ""
for f in files:
try:
bn, ext = f.rsplit(".", 1)
except ValueError:
bn, ext = f.rsplit("-", 1)
key = f"{relpath}/{bn}"
if ext == "multi":
continue
if key in ignored:
continue
if d == "invalid" and relpath == "encoding":
rv["invalid_encode"][bn] = os.path.join(root, f)
continue
if key not in rv[d]:
rv[d][key] = {}
with open(os.path.join(root, f), encoding="utf-8") as inp:
rv[d][key][ext] = inp.read()
return rv


Expand All @@ -90,6 +100,6 @@ def pytest_generate_tests(metafunc):
elif "invalid_encode_case" in metafunc.fixturenames:
metafunc.parametrize(
"invalid_encode_case",
test_list["invalid-encoder"].values(),
ids=list(test_list["invalid-encoder"].keys()),
test_list["invalid_encode"].values(),
ids=list(test_list["invalid_encode"].keys()),
)
2 changes: 1 addition & 1 deletion tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from tomlkit.items import Comment
from tomlkit.items import InlineTable
from tomlkit.items import Integer
from tomlkit.items import Key
from tomlkit.items import KeyType
from tomlkit.items import SingleKey as Key
from tomlkit.items import String
from tomlkit.items import StringType
from tomlkit.items import Table
Expand Down
10 changes: 10 additions & 0 deletions tests/test_toml_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from tomlkit import load
from tomlkit import parse
from tomlkit._compat import decode
from tomlkit._utils import parse_rfc3339
Expand All @@ -20,6 +21,9 @@ def to_bool(s):
"integer": int,
"float": float,
"datetime": parse_rfc3339,
"datetime-local": parse_rfc3339,
"date-local": parse_rfc3339,
"time-local": parse_rfc3339,
}


Expand Down Expand Up @@ -50,3 +54,9 @@ def test_valid_decode(valid_case):
def test_invalid_decode(invalid_decode_case):
with pytest.raises(TOMLKitError):
parse(invalid_decode_case["toml"])


def test_invalid_encode(invalid_encode_case):
with pytest.raises((TOMLKitError, UnicodeDecodeError)):
with open(invalid_encode_case, encoding="utf-8") as f:
load(f)
2 changes: 1 addition & 1 deletion tests/toml-test
10 changes: 5 additions & 5 deletions tomlkit/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
"^"
r"(([0-9]+)-(\d{2})-(\d{2}))?" # Date
"("
"([T ])?" # Separator
"([Tt ])?" # Separator
r"(\d{2}):(\d{2}):(\d{2})(\.([0-9]+))?" # Time
r"((Z)|([\+|\-]([01][0-9]|2[0-3]):([0-5][0-9])))?" # Timezone
r"(([Zz])|([\+|\-]([01][0-9]|2[0-3]):([0-5][0-9])))?" # Timezone
")?"
"$"
)

RFC_3339_DATETIME = re.compile(
"^"
"([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])" # Date
"[T ]" # Separator
"[Tt ]" # Separator
r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.([0-9]+))?" # Time
r"((Z)|([\+|\-]([01][0-9]|2[0-3]):([0-5][0-9])))?" # Timezone
r"(([Zz])|([\+|\-]([01][0-9]|2[0-3]):([0-5][0-9])))?" # Timezone
"$"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ def parse_rfc3339(string: str) -> Union[datetime, date, time]:
if m.group(9):
# Timezone
tz = m.group(9)
if tz == "Z":
if tz.upper() == "Z":
tzinfo = _utc
else:
sign = m.group(11)[0]
Expand Down
3 changes: 2 additions & 1 deletion tomlkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .items import Integer
from .items import Item as _Item
from .items import Key
from .items import SingleKey
from .items import String
from .items import Table
from .items import Time
Expand Down Expand Up @@ -141,7 +142,7 @@ def aot() -> AoT:


def key(k: str) -> Key:
return Key(k)
return SingleKey(k)


def value(raw: str) -> _Item:
Expand Down
21 changes: 11 additions & 10 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .items import Item
from .items import Key
from .items import Null
from .items import SingleKey
from .items import Table
from .items import Whitespace
from .items import _CustomDict
Expand Down Expand Up @@ -90,7 +91,7 @@ def add(

def append(self, key: Union[Key, str, None], item: Item) -> "Container":
if not isinstance(key, Key) and key is not None:
key = Key(key)
key = SingleKey(key)

if not isinstance(item, Item):
item = _item(item)
Expand Down Expand Up @@ -255,7 +256,7 @@ def append(self, key: Union[Key, str, None], item: Item) -> "Container":

def remove(self, key: Union[Key, str]) -> "Container":
if not isinstance(key, Key):
key = Key(key)
key = SingleKey(key)

idx = self._map.pop(key, None)
if idx is None:
Expand All @@ -281,10 +282,10 @@ def _insert_after(
raise NonExistentKey(key)

if not isinstance(key, Key):
key = Key(key)
key = SingleKey(key)

if not isinstance(other_key, Key):
other_key = Key(other_key)
other_key = SingleKey(other_key)

item = _item(item)

Expand Down Expand Up @@ -323,7 +324,7 @@ def _insert_at(self, idx: int, key: Union[Key, str], item: Any) -> "Container":
raise ValueError(f"Unable to insert at position {idx}")

if not isinstance(key, Key):
key = Key(key)
key = SingleKey(key)

item = _item(item)

Expand Down Expand Up @@ -361,7 +362,7 @@ def _insert_at(self, idx: int, key: Union[Key, str], item: Any) -> "Container":

def item(self, key: Union[Key, str]) -> Item:
if not isinstance(key, Key):
key = Key(key)
key = SingleKey(key)

idx = self._map.get(key, None)
if idx is None:
Expand Down Expand Up @@ -521,7 +522,7 @@ def __iter__(self) -> Iterator[str]:
# Dictionary methods
def __getitem__(self, key: Union[Key, str]) -> Union[Item, "Container"]:
if not isinstance(key, Key):
key = Key(key)
key = SingleKey(key)

idx = self._map.get(key, None)
if idx is None:
Expand Down Expand Up @@ -556,10 +557,10 @@ def _replace(
self, key: Union[Key, str], new_key: Union[Key, str], value: Item
) -> None:
if not isinstance(key, Key):
key = Key(key)
key = SingleKey(key)

if not isinstance(new_key, Key):
new_key = Key(new_key)
new_key = SingleKey(new_key)

idx = self._map.get(key, None)
if idx is None:
Expand All @@ -571,7 +572,7 @@ def _replace_at(
self, idx: Union[int, Tuple[int]], new_key: Union[Key, str], value: Item
) -> None:
if not isinstance(new_key, Key):
new_key = Key(new_key)
new_key = SingleKey(new_key)

if isinstance(idx, tuple):
for i in idx[1:]:
Expand Down