Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog
Unreleased
----------

- Keep top-level iterator criteria reusable in ``matches`` and ``matches_property`` predicates without consuming them eagerly.

- Fix ``to_list`` swallowing ``TypeError`` raised while consuming an iterable rather than
propagating the original failure.
- Pass only the element to builtin methods such as ``str.strip``, ``str.split``, and
Expand Down
12 changes: 11 additions & 1 deletion src/pydash/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ def matches(source: t.Any) -> t.Callable[[t.Any], bool]:
object and the `source` object, returning ``True`` if the given object has equivalent property
values, else ``False``.

Iterator sources remain reusable across repeated predicate calls.

Args:
source: Source object used for comparision.

Expand All @@ -538,7 +540,15 @@ def matches(source: t.Any) -> t.Callable[[t.Any], bool]:
.. versionchanged:: 3.0.0
Use :func:`pydash.predicates.is_match` as matching function.
"""
return lambda obj: pyd.is_match(obj, source)

def _matches(obj: t.Any) -> bool:
nonlocal source
current = source
if isinstance(source, Iterator):
source, current = tee(source)
return pyd.is_match(obj, current)

return _matches


def matches_property(key: t.Any, value: t.Any) -> t.Callable[[t.Any], bool]:
Expand Down
76 changes: 76 additions & 0 deletions tests/test_matches_iterator_reuse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest

import pydash as _


def generator_source(values):
yield from values


def make_predicate(kind, source):
if kind == "matches":
return _.matches(source)
return _.matches_property("items", source)


def candidate(kind, values):
return values if kind == "matches" else {"items": values}


@pytest.mark.parametrize("kind", ["matches", "matches_property"])
@pytest.mark.parametrize("make_source", [iter, generator_source])
def test_iterator_criteria_are_reused_for_changing_candidates(kind, make_source):
predicate = make_predicate(kind, make_source([1, 2]))

assert predicate(candidate(kind, [1, 2])) is True
assert predicate(candidate(kind, [9, 9])) is False
assert predicate(candidate(kind, [1])) is False
assert predicate(candidate(kind, [1, 2, 3])) is True
assert predicate(candidate(kind, [1, 9])) is False


@pytest.mark.parametrize("kind", ["matches", "matches_property"])
def test_iterator_criteria_remain_lazy_and_short_circuit(kind):
consumed = []

def criteria():
consumed.append(1)
yield 1
raise AssertionError("the unmatched tail should not be read")

predicate = make_predicate(kind, criteria())
assert consumed == []
assert predicate(candidate(kind, [9])) is False
assert predicate(candidate(kind, [9])) is False
assert consumed == [1]


@pytest.mark.parametrize("kind", ["matches", "matches_property"])
def test_filter_reuses_iterator_based_predicate(kind):
first = candidate(kind, [1, 2])
second = candidate(kind, [9, 9])
third = candidate(kind, [1, 2, 3])
predicate = make_predicate(kind, iter([1, 2]))

result = _.filter_([first, second, third], predicate)
assert result == [first, third]
assert result[0] is first
assert result[1] is third


@pytest.mark.parametrize("kind", ["matches", "matches_property"])
def test_empty_iterator_criteria_remain_vacuous(kind):
predicate = make_predicate(kind, iter([]))
assert predicate(candidate(kind, [])) is True
assert predicate(candidate(kind, [9])) is True


@pytest.mark.parametrize("kind", ["matches", "matches_property"])
def test_reiterable_criteria_still_reflect_mutation(kind):
source = [1, 2]
predicate = make_predicate(kind, source)
assert predicate(candidate(kind, [1, 2])) is True

source[0] = 9
assert predicate(candidate(kind, [9, 2])) is True
assert predicate(candidate(kind, [1, 2])) is False