pyochain 0.5.31__py3-none-any.whl → 0.5.32__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyochain might be problematic. Click here for more details.

@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  from collections.abc import Callable, Mapping
4
4
  from functools import partial
5
- from typing import TYPE_CHECKING, Any, TypeGuard
5
+ from typing import TYPE_CHECKING, Any, TypeIs, overload
6
6
 
7
7
  import cytoolz as cz
8
8
 
@@ -13,7 +13,13 @@ if TYPE_CHECKING:
13
13
 
14
14
 
15
15
  class FilterDict[K, V](MappingWrapper[K, V]):
16
- def filter_keys(self, predicate: Callable[[K], bool]) -> Dict[K, V]:
16
+ @overload
17
+ def filter_keys[U](self, predicate: Callable[[K], TypeIs[U]]) -> Dict[U, V]: ...
18
+ @overload
19
+ def filter_keys(self, predicate: Callable[[K], bool]) -> Dict[K, V]: ...
20
+ def filter_keys[U](
21
+ self, predicate: Callable[[K], bool | TypeIs[U]]
22
+ ) -> Dict[K, V] | Dict[U, V]:
17
23
  """
18
24
  Return keys that satisfy predicate.
19
25
 
@@ -30,7 +36,13 @@ class FilterDict[K, V](MappingWrapper[K, V]):
30
36
  """
31
37
  return self._new(partial(cz.dicttoolz.keyfilter, predicate))
32
38
 
33
- def filter_values(self, predicate: Callable[[V], bool]) -> Dict[K, V]:
39
+ @overload
40
+ def filter_values[U](self, predicate: Callable[[V], TypeIs[U]]) -> Dict[K, U]: ...
41
+ @overload
42
+ def filter_values(self, predicate: Callable[[V], bool]) -> Dict[K, V]: ...
43
+ def filter_values[U](
44
+ self, predicate: Callable[[V], bool] | Callable[[V], TypeIs[U]]
45
+ ) -> Dict[K, V] | Dict[K, U]:
34
46
  """
35
47
  Return items whose values satisfy predicate.
36
48
 
@@ -122,19 +134,19 @@ class FilterDict[K, V](MappingWrapper[K, V]):
122
134
  """
123
135
 
124
136
  def _filter_attr(data: dict[K, V]) -> dict[K, U]:
125
- def has_attr(x: V) -> TypeGuard[U]:
137
+ def has_attr(x: object) -> TypeIs[U]:
126
138
  return hasattr(x, attr)
127
139
 
128
140
  return cz.dicttoolz.valfilter(has_attr, data)
129
141
 
130
142
  return self._new(_filter_attr)
131
143
 
132
- def filter_type[R](self, typ: type[R]) -> Dict[K, R]:
144
+ def filter_type[R](self, dtype: type[R]) -> Dict[K, R]:
133
145
  """
134
146
  Filter values by type.
135
147
 
136
148
  Args:
137
- typ: Type to filter values by.
149
+ dtype: Type to filter values by.
138
150
  Example:
139
151
  ```python
140
152
  >>> import pyochain as pc
@@ -146,35 +158,13 @@ class FilterDict[K, V](MappingWrapper[K, V]):
146
158
  """
147
159
 
148
160
  def _filter_type(data: dict[K, V]) -> dict[K, R]:
149
- def _(x: V) -> TypeGuard[R]:
150
- return isinstance(x, typ)
161
+ def _(x: object) -> TypeIs[R]:
162
+ return isinstance(x, dtype)
151
163
 
152
164
  return cz.dicttoolz.valfilter(_, data)
153
165
 
154
166
  return self._new(_filter_type)
155
167
 
156
- def filter_callable(self) -> Dict[K, Callable[..., Any]]:
157
- """
158
- Filter values that are callable.
159
- ```python
160
- >>> import pyochain as pc
161
- >>> def foo():
162
- ... pass
163
- >>> data = {1: "one", 2: "two", 3: foo, 4: print}
164
- >>> pc.Dict(data).filter_callable().map_values(lambda x: x.__name__).unwrap()
165
- {3: 'foo', 4: 'print'}
166
-
167
- ```
168
- """
169
-
170
- def _filter_callable(data: dict[K, V]) -> dict[K, Callable[..., Any]]:
171
- def _(x: V) -> TypeGuard[Callable[..., Any]]:
172
- return callable(x)
173
-
174
- return cz.dicttoolz.valfilter(_, data)
175
-
176
- return self._new(_filter_callable)
177
-
178
168
  def filter_subclass[U: type[Any], R](
179
169
  self: FilterDict[K, U], parent: type[R], keep_parent: bool = True
180
170
  ) -> Dict[K, type[R]]:
@@ -205,7 +195,7 @@ class FilterDict[K, V](MappingWrapper[K, V]):
205
195
  """
206
196
 
207
197
  def _filter_subclass(data: dict[K, U]) -> dict[K, type[R]]:
208
- def _(x: type[Any]) -> TypeGuard[type[R]]:
198
+ def _(x: type[Any]) -> TypeIs[type[R]]:
209
199
  if keep_parent:
210
200
  return issubclass(x, parent)
211
201
  else:
@@ -3,7 +3,7 @@ from __future__ import annotations
3
3
  import itertools
4
4
  from collections.abc import Callable, Generator, Iterable, Iterator
5
5
  from functools import partial
6
- from typing import TYPE_CHECKING, Any, TypeGuard
6
+ from typing import TYPE_CHECKING, Any, TypeIs, overload
7
7
 
8
8
  import cytoolz as cz
9
9
  import more_itertools as mit
@@ -15,7 +15,11 @@ if TYPE_CHECKING:
15
15
 
16
16
 
17
17
  class BaseFilter[T](IterWrapper[T]):
18
- def filter(self, func: Callable[[T], bool]) -> Iter[T]:
18
+ @overload
19
+ def filter[U](self, func: Callable[[T], TypeIs[U]]) -> Iter[U]: ...
20
+ @overload
21
+ def filter(self, func: Callable[[T], bool]) -> Iter[T]: ...
22
+ def filter[U](self, func: Callable[[T], bool | TypeIs[U]]) -> Iter[T] | Iter[U]:
19
23
  """
20
24
  Return an iterator yielding those items of iterable for which function is true.
21
25
 
@@ -131,7 +135,7 @@ class BaseFilter[T](IterWrapper[T]):
131
135
  """
132
136
 
133
137
  def check(data: Iterable[Any]) -> Generator[U, None, None]:
134
- def _(x: Any) -> TypeGuard[U]:
138
+ def _(x: Any) -> TypeIs[U]:
135
139
  return hasattr(x, attr)
136
140
 
137
141
  return (x for x in data if _(x))
@@ -450,12 +454,12 @@ class BaseFilter[T](IterWrapper[T]):
450
454
 
451
455
  return self._lazy(_filter_subclass)
452
456
 
453
- def filter_type[R](self, typ: type[R]) -> Iter[R]:
457
+ def filter_type[R](self, dtype: type[R]) -> Iter[R]:
454
458
  """
455
459
  Return elements that are instances of the given type.
456
460
 
457
461
  Args:
458
- typ: Type to check against.
462
+ dtype: Type to check against.
459
463
  Example:
460
464
  ```python
461
465
  >>> import pyochain as pc
@@ -466,30 +470,10 @@ class BaseFilter[T](IterWrapper[T]):
466
470
  """
467
471
 
468
472
  def _filter_type(data: Iterable[T]) -> Generator[R, None, None]:
469
- return (x for x in data if isinstance(x, typ))
473
+ return (x for x in data if isinstance(x, dtype))
470
474
 
471
475
  return self._lazy(_filter_type)
472
476
 
473
- def filter_callable(self) -> Iter[Callable[..., Any]]:
474
- """
475
- Return only elements that are callable.
476
-
477
- Example:
478
- ```python
479
- >>> import pyochain as pc
480
- >>> pc.Iter.from_([len, 42, str, None, list]).filter_callable().into(list)
481
- [<built-in function len>, <class 'str'>, <class 'list'>]
482
-
483
- ```
484
- """
485
-
486
- def _filter_callable(
487
- data: Iterable[T],
488
- ) -> Generator[Callable[..., Any], None, None]:
489
- return (x for x in data if callable(x))
490
-
491
- return self._lazy(_filter_callable)
492
-
493
477
  def filter_map[R](self, func: Callable[[T], R]) -> Iter[R]:
494
478
  """
495
479
  Apply func to every element of iterable, yielding only those which are not None.
pyochain/_iter/_main.py CHANGED
@@ -33,6 +33,10 @@ class CommonMethods[T](BaseAgg[T], BaseEager[T], BaseDict[T]):
33
33
  pass
34
34
 
35
35
 
36
+ def _convert_data[T](data: Iterable[T] | T, *more_data: T) -> Iterable[T]:
37
+ return data if cz.itertoolz.isiterable(data) else (data, *more_data)
38
+
39
+
36
40
  class Iter[T](
37
41
  BaseBool[T],
38
42
  BaseFilter[T],
@@ -150,13 +154,7 @@ class Iter[T](
150
154
  ```
151
155
  """
152
156
 
153
- def _convert_data() -> Sequence[Any]:
154
- if cz.itertoolz.isiterable(data):
155
- return data
156
- else:
157
- return (data, *more_data)
158
-
159
- return Iter(iter(_convert_data()))
157
+ return Iter(iter(_convert_data(data, *more_data)))
160
158
 
161
159
  @staticmethod
162
160
  def unfold[S, V](seed: S, generator: Callable[[S], tuple[V, S] | None]) -> Iter[V]:
@@ -411,10 +409,7 @@ class Seq[T](CommonMethods[T]):
411
409
  ```
412
410
 
413
411
  """
414
- if cz.itertoolz.isiterable(data):
415
- return Seq(data)
416
- else:
417
- return Seq((data, *more_data))
412
+ return Seq(_convert_data(data, *more_data)) # type: ignore[return-value]
418
413
 
419
414
  def iter(self) -> Iter[T]:
420
415
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pyochain
3
- Version: 0.5.31
3
+ Version: 0.5.32
4
4
  Summary: Method chaining for iterables and dictionaries in Python.
5
5
  Requires-Dist: cytoolz>=1.0.1
6
6
  Requires-Dist: more-itertools>=10.8.0
@@ -4,7 +4,7 @@ pyochain/_core/_format.py,sha256=7H9sAlLRoUpaOw8gzKso7YAGrtcUs9dTcRvlb2oO6xo,900
4
4
  pyochain/_core/_main.py,sha256=8LPnMRJnv8SDz2Q3rmngG1rx9c7fhkgAlqm5BzlUd34,5745
5
5
  pyochain/_core/_protocols.py,sha256=UgjOCINuz6cJpkkj7--6S0ULbUudv860HVNYEquBZvM,833
6
6
  pyochain/_dict/__init__.py,sha256=z3_hkXG_BrmS63WGjQocu7QXNuWZxeFF5wAnERmobsQ,44
7
- pyochain/_dict/_filters.py,sha256=a5ohEdwZlmCQwzrCO3KM00-EsSY1wPze-jfdGM1tQLs,8111
7
+ pyochain/_dict/_filters.py,sha256=I6kmcmeUsoxjMg9mOJbRd2aKg7sjfBUUEKaiGQc2-_c,7927
8
8
  pyochain/_dict/_groups.py,sha256=2QPQsfCV7GvUTEJfpW72EDKXGw5Yvw17Pcp9-X2iBo4,6076
9
9
  pyochain/_dict/_iter.py,sha256=y8S6zAFu6A9NK-13f-u0Q3-lt16jE3kuMOl8awn_nnI,3702
10
10
  pyochain/_dict/_joins.py,sha256=a6wzbr_xAmne0ohtL5tDpdccJ_89uUKgc3MnbZr39vk,4420
@@ -17,16 +17,16 @@ pyochain/_iter/_aggregations.py,sha256=VkAYF9w4GwVBDYx1H5pL2dkMIWfodj3QsZsOc4Ach
17
17
  pyochain/_iter/_booleans.py,sha256=KE4x-lxayHH_recHoX5ZbNz7JVdC9WuvA2ewNBpqUL0,7210
18
18
  pyochain/_iter/_dicts.py,sha256=eA6WafYcOrQS-ZrUES2B-yX2HTqewSgvWMl6neqEDk8,7652
19
19
  pyochain/_iter/_eager.py,sha256=ARC995qZaEE1v9kiyZNEieM1qJKEXiOUdkIRJTpOkJs,6880
20
- pyochain/_iter/_filters.py,sha256=_ppvUG-DgEIhqmuyaOB0_g4tbtH_4bNk7vdVpjP8luY,15515
20
+ pyochain/_iter/_filters.py,sha256=IRBhvopIT8YkBdH5UEMxHz7FyADMfhkLiAB8qdleces,15136
21
21
  pyochain/_iter/_joins.py,sha256=ivvnTvfiw67U9kVWMIoy78PJNBwN0oZ4Ko9AyfxyGYM,13043
22
22
  pyochain/_iter/_lists.py,sha256=TU-HjyyM_KqJTwA_0V0fCyJHl9L6wsRi1n4Sl8g2Gro,11103
23
- pyochain/_iter/_main.py,sha256=n9ljv7tL0U58QUpOkwFbesd-9Vd5dwJv-pV0JLmJle8,15047
23
+ pyochain/_iter/_main.py,sha256=ILGWDv6E0PWYDkeZEAwHE8chjow9xcosVH6M94Mjb5I,14986
24
24
  pyochain/_iter/_maps.py,sha256=5PR7OGX9VewX_CDRyllcg0wIKEu81yaQZU7sRU6OCs4,11914
25
25
  pyochain/_iter/_partitions.py,sha256=MYxlzQRrCBtfjnhtIVdMUhkNq5FCTpFV1R9sJ9LznsM,5095
26
26
  pyochain/_iter/_process.py,sha256=P3Zw3uInZkOL-VlDUG4xfTnwek6lIa4j2a3IwxVaLD0,11039
27
27
  pyochain/_iter/_rolling.py,sha256=YJ5X23eZTizXEJYneaZvn98zORbvJzLWXP8gX1BCvGY,6979
28
28
  pyochain/_iter/_tuples.py,sha256=rcEeqrz3eio1CEYyZ0lt2CC5P_OW7ARLkTL0g7yf3ws,11137
29
29
  pyochain/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
- pyochain-0.5.31.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
31
- pyochain-0.5.31.dist-info/METADATA,sha256=nLD9GiLrdJM5PEjBFHx0qVI2B4Sgt5m-c3gDqtI-I-I,9985
32
- pyochain-0.5.31.dist-info/RECORD,,
30
+ pyochain-0.5.32.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
31
+ pyochain-0.5.32.dist-info/METADATA,sha256=VX2CxM_dLgjRivIc5_LwCJ5QBQmjDjNFjwG2CYjGVXg,9985
32
+ pyochain-0.5.32.dist-info/RECORD,,