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

fix: Build table with dotted key correctly #284

Merged
merged 3 commits into from
Apr 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.3.0
hooks:
- id: black

Expand Down Expand Up @@ -37,7 +37,7 @@ repos:
exclude: ^tomlkit/items\.py

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.3.2
hooks:
- id: pyupgrade
args: [--py37-plus]
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
name = "tomlkit"
version = "0.11.7"
description = "Style preserving TOML library"
authors = ["Sébastien Eustace <sebastien@eustace.io>"]
authors = [
"Sébastien Eustace <sebastien@eustace.io>",
"Frost Ming <me@frostming.com>"
]
license = "MIT"

readme = "README.md"
Expand Down
29 changes: 29 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,32 @@ def test_nested_table_update_display_name():
z = 3
"""
assert doc.as_string() == dedent(expected)


def test_build_table_with_dotted_key():
doc = tomlkit.document()
data = {
"a.b.c": 1,
"a.b.d": 2,
"a": {"c": {"foo": "bar"}},
"a.d.e": 3,
}

for key, value in data.items():
if "." not in key:
doc.append(key, value)
else:
doc.append(tomlkit.key(key.split(".")), value)

expected = """\
a.b.c = 1
a.b.d = 2
a.d.e = 3

[a.c]
foo = "bar"
"""
assert doc.as_string() == expected
assert json.loads(json.dumps(doc)) == {
"a": {"b": {"c": 1, "d": 2}, "d": {"e": 3}, "c": {"foo": "bar"}}
}
6 changes: 3 additions & 3 deletions tomlkit/_compat.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations

import contextlib
import sys

from typing import Any
from typing import List
from typing import Optional


PY38 = sys.version_info >= (3, 8)


def decode(string: Any, encodings: Optional[List[str]] = None):
def decode(string: Any, encodings: list[str] | None = None):
if not isinstance(string, bytes):
return string

Expand Down
5 changes: 3 additions & 2 deletions tomlkit/_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

from collections.abc import Mapping
Expand All @@ -7,7 +9,6 @@
from datetime import timedelta
from datetime import timezone
from typing import Collection
from typing import Union

from tomlkit._compat import decode

Expand Down Expand Up @@ -41,7 +42,7 @@
_utc = timezone(timedelta(), "UTC")


def parse_rfc3339(string: str) -> Union[datetime, date, time]:
def parse_rfc3339(string: str) -> datetime | date | time:
m = RFC_3339_DATETIME.match(string)
if m:
year = int(m.group(1))
Expand Down
21 changes: 10 additions & 11 deletions tomlkit/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

import datetime as _datetime

from collections.abc import Mapping
from typing import IO
from typing import Iterable
from typing import Optional
from typing import Tuple
from typing import Union

from tomlkit._utils import parse_rfc3339
from tomlkit.container import Container
Expand Down Expand Up @@ -34,7 +33,7 @@
from tomlkit.toml_document import TOMLDocument


def loads(string: Union[str, bytes]) -> TOMLDocument:
def loads(string: str | bytes) -> TOMLDocument:
"""
Parses a string into a TOMLDocument.

Expand All @@ -59,7 +58,7 @@ def dumps(data: Mapping, sort_keys: bool = False) -> str:
raise TypeError(msg) from ex


def load(fp: Union[IO[str], IO[bytes]]) -> TOMLDocument:
def load(fp: IO[str] | IO[bytes]) -> TOMLDocument:
"""
Load toml document from a file-like object.
"""
Expand All @@ -76,7 +75,7 @@ def dump(data: Mapping, fp: IO[str], *, sort_keys: bool = False) -> None:
fp.write(dumps(data, sort_keys=sort_keys))


def parse(string: Union[str, bytes]) -> TOMLDocument:
def parse(string: str | bytes) -> TOMLDocument:
"""
Parses a string or bytes into a TOMLDocument.
"""
Expand All @@ -91,12 +90,12 @@ def document() -> TOMLDocument:


# Items
def integer(raw: Union[str, int]) -> Integer:
def integer(raw: str | int) -> Integer:
"""Create an integer item from a number or string."""
return item(int(raw))


def float_(raw: Union[str, float]) -> Float:
def float_(raw: str | float) -> Float:
"""Create an float item from a number or string."""
return item(float(raw))

Expand Down Expand Up @@ -175,7 +174,7 @@ def array(raw: str = None) -> Array:
return value(raw)


def table(is_super_table: Optional[bool] = None) -> Table:
def table(is_super_table: bool | None = None) -> Table:
"""Create an empty table.

:param is_super_table: if true, the table is a super table
Expand Down Expand Up @@ -224,7 +223,7 @@ def aot() -> AoT:
return AoT([])


def key(k: Union[str, Iterable[str]]) -> Key:
def key(k: str | Iterable[str]) -> Key:
"""Create a key from a string. When a list of string is given,
it will create a dotted key.

Expand Down Expand Up @@ -261,7 +260,7 @@ def value(raw: str) -> _Item:
return v


def key_value(src: str) -> Tuple[Key, _Item]:
def key_value(src: str) -> tuple[Key, _Item]:
"""Parse a key-value pair from a string.

:Example:
Expand Down
Loading