Skip to content

Commit

Permalink
Improve None type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
moomoohk authored Jun 12, 2024
1 parent a13283b commit 74b4b7e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions dpath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Needed for pre-3.10 versions
from __future__ import annotations

__all__ = [
"new",
"delete",
Expand Down Expand Up @@ -48,7 +51,7 @@ def _split_path(path: Path, separator: Optional[str] = "/") -> Union[List[PathSe
return split_segments


def new(obj: MutableMapping, path: Path, value, separator="/", creator: Creator = None) -> MutableMapping:
def new(obj: MutableMapping, path: Path, value, separator="/", creator: Creator | None = None) -> MutableMapping:
"""
Set the element at the terminus of path to value, and create
it if it does not exist (as opposed to 'set' that can only
Expand All @@ -68,7 +71,7 @@ def new(obj: MutableMapping, path: Path, value, separator="/", creator: Creator
return segments.set(obj, split_segments, value)


def delete(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter = None) -> int:
def delete(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter | None = None) -> int:
"""
Given a obj, delete all elements that match the glob.
Expand Down Expand Up @@ -127,7 +130,7 @@ def f(obj, pair, counter):
return deleted


def set(obj: MutableMapping, glob: Glob, value, separator="/", afilter: Filter = None) -> int:
def set(obj: MutableMapping, glob: Glob, value, separator="/", afilter: Filter | None = None) -> int:
"""
Given a path glob, set all existing elements in the document
to the given value. Returns the number of elements changed.
Expand Down Expand Up @@ -193,7 +196,7 @@ def f(_, pair, results):
return results[0]


def values(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter = None, dirs=True):
def values(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter | None = None, dirs=True):
"""
Given an object and a path glob, return an array of all values which match
the glob. The arguments to this function are identical to those of search().
Expand All @@ -203,13 +206,13 @@ def values(obj: MutableMapping, glob: Glob, separator="/", afilter: Filter = Non
return [v for p, v in search(obj, glob, yielded, separator, afilter, dirs)]


def search(obj: MutableMapping, glob: Glob, yielded=False, separator="/", afilter: Filter = None, dirs=True):
def search(obj: MutableMapping, glob: Glob, yielded=False, separator="/", afilter: Filter | None = None, dirs=True):
"""
Given a path glob, return a dictionary containing all keys
that matched the given glob.
If 'yielded' is true, then a dictionary will not be returned.
Instead tuples will be yielded in the form of (path, value) for
Instead, tuples will be yielded in the form of (path, value) for
every element in the document that matched the glob.
"""

Expand All @@ -218,7 +221,7 @@ def search(obj: MutableMapping, glob: Glob, yielded=False, separator="/", afilte
def keeper(path, found):
"""
Generalized test for use in both yielded and folded cases.
Returns True if we want this result. Otherwise returns False.
Returns True if we want this result. Otherwise, returns False.
"""
if not dirs and not segments.leaf(found):
return False
Expand All @@ -245,7 +248,13 @@ def f(obj, pair, result):
return segments.fold(obj, f, {})


def merge(dst: MutableMapping, src: MutableMapping, separator="/", afilter: Filter = None, flags=MergeType.ADDITIVE):
def merge(
dst: MutableMapping,
src: MutableMapping,
separator="/",
afilter: Filter | None = None,
flags=MergeType.ADDITIVE
):
"""
Merge source into destination. Like dict.update() but performs deep
merging.
Expand Down

0 comments on commit 74b4b7e

Please sign in to comment.