2222from typing import TypeVar
2323from typing import overload
2424
25+ from typing_extensions import Self
26+
2527from tomlkit ._compat import PY38
2628from tomlkit ._compat import decode
2729from tomlkit ._types import _CustomDict
@@ -437,7 +439,7 @@ def is_bare(self) -> bool:
437439 def __hash__ (self ) -> int :
438440 return hash (self .key )
439441
440- def __eq__ (self , other : Any ) -> bool :
442+ def __eq__ (self , other : object ) -> bool :
441443 if isinstance (other , Key ):
442444 return isinstance (other , SingleKey ) and self .key == other .key
443445
@@ -1599,8 +1601,7 @@ def insert(self, pos: int, value: Any) -> None: # type: ignore[override]
15991601 list .insert (self , pos , it )
16001602 if pos < 0 :
16011603 pos += length
1602- if pos < 0 :
1603- pos = 0
1604+ pos = max (pos , 0 )
16041605
16051606 idx = 0 # insert position of the self._value list
16061607 default_indent = " "
@@ -1758,7 +1759,7 @@ def append(self: AT, key: None, value: Comment | Whitespace) -> AT: ...
17581759 @overload
17591760 def append (self : AT , key : Key | str , value : Any ) -> AT : ...
17601761
1761- def append (self : AT , key : Key | str | None , value : Any ) -> AT :
1762+ def append (self , key : Key | str | None , value : Any ) -> Self :
17621763 raise NotImplementedError
17631764
17641765 @overload
@@ -1768,8 +1769,8 @@ def add(self: AT, key: Comment | Whitespace) -> AT: ...
17681769 def add (self : AT , key : Key | str , value : Any = ...) -> AT : ...
17691770
17701771 def add (
1771- self : AT , key : Key | str | Comment | Whitespace , value : Any | None = None
1772- ) -> AT :
1772+ self , key : Key | str | Comment | Whitespace , value : Any | None = None
1773+ ) -> Self :
17731774 if value is None :
17741775 if not isinstance (key , (Comment , Whitespace )):
17751776 msg = "Non comment/whitespace items must have an associated key"
@@ -1782,7 +1783,7 @@ def add(
17821783
17831784 return self .append (key , value )
17841785
1785- def remove (self : AT , key : Key | str ) -> AT :
1786+ def remove (self , key : Key | str ) -> Self :
17861787 self ._value .remove (key )
17871788
17881789 if isinstance (key , Key ):
@@ -1803,7 +1804,7 @@ def setdefault(self, key: Key | str, default: Any) -> Any: # type: ignore[overr
18031804 def __str__ (self ) -> str :
18041805 return str (self .value )
18051806
1806- def copy (self : AT ) -> AT :
1807+ def copy (self ) -> Self :
18071808 return copy .copy (self )
18081809
18091810 def __repr__ (self ) -> str :
0 commit comments