Skip to content

Commit 94e62fb

Browse files
dimblebyCopilot
andauthored
Float is not a sequence (#563)
* Remove scalar indexing fallbacks * Clean up obsolete override suppressions --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 495a42e commit 94e62fb

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

‎tests/test_items.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

33
import copy
4+
import ctypes
45
import math
56
import pickle
7+
import sys
68

79
from collections.abc import Callable
810
from datetime import date
@@ -101,6 +103,18 @@ def test_float_unwrap() -> None:
101103
elementary_test(item(2.78), float)
102104

103105

106+
@pytest.mark.skipif(
107+
sys.implementation.name != "cpython", reason="PySequence_Check is CPython-specific"
108+
)
109+
def test_float_is_not_a_sequence() -> None:
110+
value = parse("a = [1.0, 2.0, 3.0]")["a"][0]
111+
py_sequence_check = ctypes.pythonapi.PySequence_Check
112+
py_sequence_check.argtypes = [ctypes.py_object]
113+
py_sequence_check.restype = ctypes.c_int
114+
115+
assert not py_sequence_check(value)
116+
117+
104118
def test_false_unwrap() -> None:
105119
elementary_test(item(False), bool)
106120

‎tomlkit/items.py‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,6 @@ def __reduce__(self) -> tuple[type, tuple[object, ...]]:
540540
def __reduce_ex__(self, protocol: int) -> tuple[type, tuple[object, ...]]: # type: ignore[override]
541541
return self.__class__, self._getstate(protocol)
542542

543-
def __getitem__(self, key: Key | str | int) -> Any:
544-
raise TypeError(f"{type(self).__name__} does not support item access")
545-
546-
def __setitem__(self, key: Key | str | int, value: Any) -> None:
547-
raise TypeError(f"{type(self).__name__} does not support item assignment")
548-
549-
def __delitem__(self, key: Key | str | int) -> None:
550-
raise TypeError(f"{type(self).__name__} does not support item deletion")
551-
552543

553544
class Whitespace(Item):
554545
"""
@@ -1824,10 +1815,10 @@ def __iter__(self) -> Iterator[str]:
18241815
def __len__(self) -> int:
18251816
return len(self._value)
18261817

1827-
def __delitem__(self, key: Key | str) -> None: # type: ignore[override]
1818+
def __delitem__(self, key: Key | str) -> None:
18281819
self.remove(key)
18291820

1830-
def __getitem__(self, key: Key | str) -> Any: # type: ignore[override]
1821+
def __getitem__(self, key: Key | str) -> Any:
18311822
return self._value[key]
18321823

18331824
def __contains__(self, key: object) -> bool:
@@ -1839,7 +1830,7 @@ def __contains__(self, key: object) -> bool:
18391830
# for an out-of-order entry, so validation runs exactly as before.
18401831
return key in self._value
18411832

1842-
def __setitem__(self, key: Key | str, value: Any) -> None: # type: ignore[override]
1833+
def __setitem__(self, key: Key | str, value: Any) -> None:
18431834
if not isinstance(value, Item):
18441835
value = item(value, _parent=self)
18451836

@@ -2181,7 +2172,7 @@ def _render_dotted(self, key: Key, table: Table) -> list[str]:
21812172
)
21822173
return parts
21832174

2184-
def __setitem__(self, key: Key | str, value: Any) -> None: # type: ignore[override]
2175+
def __setitem__(self, key: Key | str, value: Any) -> None:
21852176
if hasattr(value, "trivia") and value.trivia.comment:
21862177
value.trivia.comment = ""
21872178
if not isinstance(value, Item):
@@ -2196,7 +2187,7 @@ def _getstate(self, protocol: int = 3) -> tuple[container.Container, Trivia]:
21962187
return (self._value, self._trivia)
21972188

21982189

2199-
class String(str, Item): # type: ignore[misc]
2190+
class String(str, Item):
22002191
"""
22012192
A string literal.
22022193
"""

0 commit comments

Comments
 (0)