Skip to content

Commit

Permalink
make to_path_tokens always return a list of PathToken
Browse files Browse the repository at this point in the history
  • Loading branch information
DeviousStoat committed Jan 27, 2024
1 parent 7e876c7 commit 1f391a1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
12 changes: 4 additions & 8 deletions src/pydash/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2408,9 +2408,6 @@ def update_with(obj, path, updater, customizer=None): # noqa: C901
default_type = dict if isinstance(obj, dict) else list
tokens = to_path_tokens(path)

if not pyd.is_list(tokens): # pragma: no cover
tokens = [tokens]

last_key = pyd.last(tokens)

if isinstance(last_key, PathToken):
Expand Down Expand Up @@ -2480,9 +2477,6 @@ def unset(obj: t.Union[t.List, t.Dict], path: PathT) -> bool: # noqa: C901
"""
tokens = to_path_tokens(path)

if not pyd.is_list(tokens): # pragma: no cover
tokens = [tokens]

last_key = pyd.last(tokens)

if isinstance(last_key, PathToken):
Expand Down Expand Up @@ -2510,10 +2504,12 @@ def unset(obj: t.Union[t.List, t.Dict], path: PathT) -> bool: # noqa: C901
if target is not UNSET:
try:
try:
target.pop(last_key)
# last_key can be a lot of things
# safe as everything wrapped in try/except
target.pop(last_key) # type: ignore
did_unset = True
except TypeError:
target.pop(int(last_key))
target.pop(int(last_key)) # type: ignore
did_unset = True
except Exception:
pass
Expand Down
12 changes: 3 additions & 9 deletions src/pydash/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,13 +1420,7 @@ def to_path(value: PathT) -> t.List[t.Hashable]:
.. versionchanged:: 4.2.1
Ensure returned path is always a list.
"""
tokens = to_path_tokens(value)
if isinstance(tokens, list):
path = [
token.key if isinstance(token, PathToken) else token for token in to_path_tokens(value)
]
else:
path = [tokens]
path = [token.key for token in to_path_tokens(value)]
return path


Expand Down Expand Up @@ -1485,7 +1479,7 @@ def _to_path_token(key) -> PathToken:
)


def to_path_tokens(value):
def to_path_tokens(value) -> list[PathToken]:
"""Parse `value` into :class:`PathToken` objects."""
if pyd.is_string(value) and ("." in value or "[" in value):
# Since we can't tell whether a bare number is supposed to be dict key or a list index, we
Expand All @@ -1499,7 +1493,7 @@ def to_path_tokens(value):
elif pyd.is_list(value):
keys = [_to_path_token(key) for key in value]
else:
keys = value
keys = [_to_path_token(value)]

return keys

Expand Down

0 comments on commit 1f391a1

Please sign in to comment.