Skip to content
Open
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ Contributors
- wynneyin, `wynneyin@github <https://github.com/wynneyin>`_
- Arin Aeruva, `arin30@github <https://github.com/arin30>`_
- Cyber Marty, `Cyber-Marty@github <https://github.com/Cyber-Marty>`_
- ainuo-AI, `ainuo-AI@github <https://github.com/ainuo-AI>`_
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
----------

- Group mapping values rather than keys in ``group_by``, including when using
iteratee shorthands, and infer the grouped value types from the mapping.
- Stop ``partial`` and ``partial_right`` wrappers from accepting a positional iteratee argument in
a slot that a keyword argument already binds, so ``map_`` passes the element instead of an index
into the bound parameter.
Expand Down
17 changes: 17 additions & 0 deletions src/pydash/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,18 @@ def for_each_right(collection, iteratee):
return collection


@t.overload
def group_by(
collection: t.Mapping[t.Any, T], iteratee: t.Callable[[T], T2]
) -> t.Dict[T2, t.List[T]]: ...


@t.overload
def group_by(
collection: t.Mapping[t.Any, T], iteratee: t.Union[IterateeObjT, None] = None
) -> t.Dict[t.Any, t.List[T]]: ...


@t.overload
def group_by(collection: t.Iterable[T], iteratee: t.Callable[[T], T2]) -> t.Dict[T2, t.List[T]]: ...

Expand Down Expand Up @@ -870,12 +882,17 @@ def group_by(collection, iteratee=None):
>>> results = group_by([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], {'a': 1})
>>> assert results == {False: [{'a': 3, 'b': 4}],\
True: [{'a': 1, 'b': 2}]}
>>> group_by({'first': 1, 'second': 3}, lambda value: value % 2)
{1: [1, 3]}

.. versionadded:: 1.0.0
"""
ret = {}
cbk = pyd.iteratee(iteratee)

if isinstance(collection, Mapping):
collection = collection.values()

for value in collection:
key = cbk(value)
ret.setdefault(key, [])
Expand Down
19 changes: 19 additions & 0 deletions tests/pytest_mypy_testing/test_group_by_mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import typing as t

import pytest
from typing_extensions import assert_type

import pydash as _


@pytest.mark.mypy_testing
def test_mypy_group_by_mappings() -> None:
def parity(value: int) -> int:
return value % 2

values: t.Mapping[str, int] = {"first": 1, "second": 3}
assert_type(_.group_by(values, parity), t.Dict[int, t.List[int]])
assert_type(_.group_by(values), t.Dict[t.Any, t.List[int]])

records: t.Mapping[str, t.Dict[str, int]] = {"first": {"kind": 1}}
assert_type(_.group_by(records, "kind"), t.Dict[t.Any, t.List[t.Dict[str, int]]])
34 changes: 34 additions & 0 deletions tests/test_group_by_mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from collections import UserDict
from types import MappingProxyType

import pytest

import pydash as _


@pytest.mark.parametrize("factory", [dict, UserDict, MappingProxyType])
@pytest.mark.parametrize(
"collection,iteratee,expected",
[
({"first": 1, "second": 2, "third": 1}, None, {1: [1, 1], 2: [2]}),
(
{"first": 1, "second": 2, "third": 3},
lambda value: value % 2,
{1: [1, 3], 0: [2]},
),
(
{"first": {"kind": "a"}, "second": {"kind": "b"}, "third": {"kind": "a"}},
"kind",
{"a": [{"kind": "a"}, {"kind": "a"}], "b": [{"kind": "b"}]},
),
(
{"first": {"kind": "a"}, "second": {"kind": "b"}},
{"kind": "a"},
{True: [{"kind": "a"}], False: [{"kind": "b"}]},
),
],
)
def test_group_by_mapping_values(factory, collection, iteratee, expected):
mapping = factory(collection)
assert _.group_by(mapping, iteratee) == expected
assert dict(mapping) == collection
Loading