New GitHub issue #93156 from ramwin:<br>
<hr>
<pre>
**Bug report**
the `pathlib.Path.parents[-1]` didn't receive the `self._parts`
example:
```
[#6#wangx@manjaro-5800:~] $ python
Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> Path("/home/wangx").parents[-1]
PosixPath('/')
>>> Path("/home/wangx").parents[-1] == Path("/")
False
>>> Path("/home/wangx").parents[1] == Path("/")
True
>>> Path("/home/wangx").parents[1]
PosixPath('/')
>>>
```
source code in `pathlib.py`:
```
422 class _PathParents(Sequence):
423 """This object provides sequence-like access to the logical ancestors
424 of a path. Don't try to construct it yourself."""
425 __slots__ = ('_pathcls', '_drv', '_root', '_parts')
426
427 def __init__(self, path):
428 # We don't store the instance to avoid reference cycles
429 self._pathcls = type(path)
430 self._drv = path._drv
431 self._root = path._root
432 self._parts = path._parts
433
434 def __len__(self):
435 if self._drv or self._root:
436 return len(self._parts) - 1
437 else:
438 return len(self._parts)
439
440 def __getitem__(self, idx):
441 if isinstance(idx, slice):
442 return tuple(self[i] for i in range(*idx.indices(len(self))))
443
444 if idx >= len(self) or idx < -len(self):
445 raise IndexError(idx)
446 return self._pathcls._from_parsed_parts(self._drv, self._root,
447 self._parts[:-idx - 1])
```
when the idx is -1, `-idx - 1 = 0`. so the `self._parts[: -idx -1 ]` is empty. hence then the `parents[-1] != parents[2]` because the `_parts` is different.
**Your environment**
<!-- Include as many relevant details as possible about the environment you experienced the bug in -->
- CPython versions tested on: 3.10.4 and 3.12.0a0(main branch)
- Operating system and architecture: `Manjaro Linux 21.2.6 Linux manjaro-5800 5.4.195-1-MANJARO #1 SMP PREEMPT Wed May 18 09:23:31 UTC 2022 x86_64 GNU/Linux`
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/93156">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>