Skip to content
Open
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
args: [--py39-plus]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.21
rev: v0.16.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import json
import os

from collections.abc import Callable
from datetime import date
from datetime import datetime
from datetime import time
from types import MappingProxyType
from typing import Any
from typing import Callable

import pytest

Expand Down
2 changes: 1 addition & 1 deletion tests/test_toml_tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import os

from collections.abc import Callable
from typing import Any
from typing import Callable

import pytest

Expand Down
7 changes: 3 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import time
from datetime import timedelta as td
from datetime import timezone as tz
from typing import Union

import pytest

Expand Down Expand Up @@ -35,18 +34,18 @@
),
],
)
def test_parse_rfc3339_datetime(string: str, expected: Union[dt, date, time]) -> None:
def test_parse_rfc3339_datetime(string: str, expected: dt | date | time) -> None:
assert parse_rfc3339(string) == expected


@pytest.mark.parametrize("string, expected", [("1979-05-27", date(1979, 5, 27))])
def test_parse_rfc3339_date(string: str, expected: Union[dt, date, time]) -> None:
def test_parse_rfc3339_date(string: str, expected: dt | date | time) -> None:
assert parse_rfc3339(string) == expected


@pytest.mark.parametrize(
"string, expected",
[("12:34:56", time(12, 34, 56)), ("12:34:56.123456", time(12, 34, 56, 123456))],
)
def test_parse_rfc3339_time(string: str, expected: Union[dt, date, time]) -> None:
def test_parse_rfc3339_time(string: str, expected: dt | date | time) -> None:
assert parse_rfc3339(string) == expected
6 changes: 4 additions & 2 deletions tomlkit/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import Any
from typing import TypeVar

from typing_extensions import Self


WT = TypeVar("WT", bound="WrapperType")

Expand Down Expand Up @@ -31,15 +33,15 @@
from builtins import float as _CustomFloat
from builtins import int as _CustomInt
from builtins import list as _CustomList
from typing import Callable
from collections.abc import Callable
from typing import Concatenate
from typing import ParamSpec
from typing import Protocol

P = ParamSpec("P")

class WrapperType(Protocol):
def _new(self: WT, value: Any) -> WT: ...
def _new(self, value: Any) -> Self: ...

else:
from collections.abc import MutableMapping
Expand Down
2 changes: 1 addition & 1 deletion tomlkit/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ def merge_dicts(d1: dict[str, Any], d2: dict[str, Any]) -> None:
if k in d1 and isinstance(d1[k], dict) and isinstance(v, Mapping):
merge_dicts(d1[k], dict(v))
else:
d1[k] = d2[k]
d1[k] = v
3 changes: 1 addition & 2 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def _handle_dotted_key(self, key: Key, value: Item) -> None:
current.append(last, value)

self.append(name, table)
return

def _get_last_index_before_table(self) -> int:
last_index = -1
Expand Down Expand Up @@ -1278,7 +1277,7 @@ def _equal_with_nan(left: Any, right: Any) -> bool:
if isinstance(left, list) and isinstance(right, list):
if len(left) != len(right):
return False
return all(_equal_with_nan(l, r) for l, r in zip(left, right)) # noqa: B905, E741
return all(_equal_with_nan(l, r) for l, r in zip(left, right)) # noqa: B905

if isinstance(left, float) and isinstance(right, float):
if math.isnan(left) and math.isnan(right):
Expand Down
17 changes: 9 additions & 8 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from typing import TypeVar
from typing import overload

from typing_extensions import Self

from tomlkit._compat import PY38
from tomlkit._compat import decode
from tomlkit._types import _CustomDict
Expand Down Expand Up @@ -437,7 +439,7 @@ def is_bare(self) -> bool:
def __hash__(self) -> int:
return hash(self.key)

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, Key):
return isinstance(other, SingleKey) and self.key == other.key

Expand Down Expand Up @@ -1599,8 +1601,7 @@ def insert(self, pos: int, value: Any) -> None: # type: ignore[override]
list.insert(self, pos, it)
if pos < 0:
pos += length
if pos < 0:
pos = 0
pos = max(pos, 0)

idx = 0 # insert position of the self._value list
default_indent = " "
Expand Down Expand Up @@ -1758,7 +1759,7 @@ def append(self: AT, key: None, value: Comment | Whitespace) -> AT: ...
@overload
def append(self: AT, key: Key | str, value: Any) -> AT: ...

def append(self: AT, key: Key | str | None, value: Any) -> AT:
def append(self, key: Key | str | None, value: Any) -> Self:
raise NotImplementedError

@overload
Expand All @@ -1768,8 +1769,8 @@ def add(self: AT, key: Comment | Whitespace) -> AT: ...
def add(self: AT, key: Key | str, value: Any = ...) -> AT: ...

def add(
self: AT, key: Key | str | Comment | Whitespace, value: Any | None = None
) -> AT:
self, key: Key | str | Comment | Whitespace, value: Any | None = None
) -> Self:
if value is None:
if not isinstance(key, (Comment, Whitespace)):
msg = "Non comment/whitespace items must have an associated key"
Expand All @@ -1782,7 +1783,7 @@ def add(

return self.append(key, value)

def remove(self: AT, key: Key | str) -> AT:
def remove(self, key: Key | str) -> Self:
self._value.remove(key)

if isinstance(key, Key):
Expand All @@ -1803,7 +1804,7 @@ def setdefault(self, key: Key | str, default: Any) -> Any: # type: ignore[overr
def __str__(self) -> str:
return str(self.value)

def copy(self: AT) -> AT:
def copy(self) -> Self:
return copy.copy(self)

def __repr__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tomlkit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import re
import string

from collections.abc import Callable
from typing import Any
from typing import Callable

from tomlkit._compat import decode
from tomlkit._utils import RFC_3339_LOOSE
Expand Down
Loading