Skip to content
Merged
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
21 changes: 11 additions & 10 deletions pathable/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
from pathable.types import LookupNode
from pathable.types import LookupValue

# Python 3.11+ shortcut: typing.Self
TBasePath = TypeVar("TBasePath", bound="BasePath")

TAccessorPath = TypeVar("TAccessorPath", bound="AccessorPath[Any, Any, Any]")

@dataclass(frozen=True, init=False)
class BasePath:
Expand Down Expand Up @@ -128,11 +129,11 @@ def __init__(self, accessor: NodeAccessor[N, K, V], *args: Any, separator: Optio

@classmethod
def _from_parsed_parts(
cls: Type["AccessorPath[N, K, V]"],
cls: Type[TAccessorPath],
parts: tuple[Hashable, ...],
separator: Optional[str] = None,
accessor: Union[NodeAccessor[N, K, V], None] = None,
) -> "AccessorPath[N, K, V]":
) -> TAccessorPath:
if accessor is None:
raise ValueError("accessor must be provided")
instance = cls.__new__(cls)
Expand All @@ -141,24 +142,24 @@ def _from_parsed_parts(
object.__setattr__(instance, 'accessor', accessor)
return instance

def _make_child(self: "AccessorPath[N, K, V]", args: list[Any]) -> "AccessorPath[N, K, V]":
def _make_child(self: TAccessorPath, args: list[Any]) -> TAccessorPath:
parts = parse_args(args, self.separator)
parts_joined = self.parts + parts
return self._from_parsed_parts(
parts_joined, separator=self.separator, accessor=self.accessor,
)

def _make_child_relpath(
self: "AccessorPath[N, K, V]", part: Hashable
) -> "AccessorPath[N, K, V]":
self: TAccessorPath, part: Hashable
) -> TAccessorPath:
# This is an optimization used for dir walking. `part` must be
# a single part relative to this path.
parts = self.parts + (part, )
return self._from_parsed_parts(
parts, separator=self.separator, accessor=self.accessor,
)

def __iter__(self: "AccessorPath[N, K, V]") -> Iterator["AccessorPath[N, K, V]"]:
def __iter__(self: TAccessorPath) -> Iterator[TAccessorPath]:
for key in self.accessor.keys(self.parts):
yield self._make_child_relpath(key)

Expand Down Expand Up @@ -189,23 +190,23 @@ def getkey(self, key: K, default: Any = None) -> Any:
except KeyError:
return default

def iter(self: "AccessorPath[N, K, V]") -> Iterator["AccessorPath[N, K, V]"]:
def iter(self: TAccessorPath) -> Iterator[TAccessorPath]:
"""Iterate over all child paths."""
warnings.warn(
"'iter' method is deprecated. Use 'iter(path)' instead.",
DeprecationWarning,
)
return iter(self)

def iteritems(self: "AccessorPath[N, K, V]") -> Iterator[tuple[Any, "AccessorPath[N, K, V]"]]:
def iteritems(self: TAccessorPath) -> Iterator[tuple[K, TAccessorPath]]:
"""Return path's items."""
warnings.warn(
"'iteritems' method is deprecated. Use 'items' instead.",
DeprecationWarning,
)
return self.items()

def items(self: "AccessorPath[N, K, V]") -> Iterator[tuple[Any, "AccessorPath[N, K, V]"]]:
def items(self: TAccessorPath) -> Iterator[tuple[K, TAccessorPath]]:
"""Return path's items."""
for key in self.accessor.keys(self.parts):
yield key, self._make_child_relpath(key)
Expand Down
Loading