diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4d5cfc1..d279a09 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] diff --git a/tests/test_api.py b/tests/test_api.py index b5f31bc..66bd702 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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 diff --git a/tests/test_toml_tests.py b/tests/test_toml_tests.py index 667353e..5358d5e 100644 --- a/tests/test_toml_tests.py +++ b/tests/test_toml_tests.py @@ -1,8 +1,8 @@ import json import os +from collections.abc import Callable from typing import Any -from typing import Callable import pytest diff --git a/tests/test_utils.py b/tests/test_utils.py index c4f96cf..f412cac 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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 @@ -35,12 +34,12 @@ ), ], ) -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 @@ -48,5 +47,5 @@ def test_parse_rfc3339_date(string: str, expected: Union[dt, date, time]) -> Non "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 diff --git a/tomlkit/_types.py b/tomlkit/_types.py index 3585d56..013e2d6 100644 --- a/tomlkit/_types.py +++ b/tomlkit/_types.py @@ -4,6 +4,8 @@ from typing import Any from typing import TypeVar +from typing_extensions import Self + WT = TypeVar("WT", bound="WrapperType") @@ -31,7 +33,7 @@ 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 @@ -39,7 +41,7 @@ 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 diff --git a/tomlkit/_utils.py b/tomlkit/_utils.py index 3c20574..f6217d3 100644 --- a/tomlkit/_utils.py +++ b/tomlkit/_utils.py @@ -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 diff --git a/tomlkit/container.py b/tomlkit/container.py index 8ff30d9..476638b 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -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 @@ -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): diff --git a/tomlkit/items.py b/tomlkit/items.py index 31369b0..4c7def5 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -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 @@ -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 @@ -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 = " " @@ -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 @@ -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" @@ -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): @@ -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: diff --git a/tomlkit/parser.py b/tomlkit/parser.py index c97a08d..a248fbd 100644 --- a/tomlkit/parser.py +++ b/tomlkit/parser.py @@ -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