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: 1 addition & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build and publish to pypi
uses: JRubics/poetry-publish@v2.1
uses: JRubics/poetry-publish@4b3306307f536bbfcb559603629b3b4f6aef5ab8
with:
pypi_token: ${{ secrets.PYPI_API_TOKEN }}
6 changes: 3 additions & 3 deletions pystreamapi/_itertools/tools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# pylint: disable=protected-access
from typing import Iterable
from typing import Iterable, Optional

from pystreamapi._streams.error.__error import ErrorHandler, _sentinel


def dropwhile(predicate, iterable, handler: ErrorHandler = None):
def dropwhile(predicate, iterable, handler: Optional[ErrorHandler] = None):
"""
Drop items from the iterable while predicate(item) is true.
Afterward, return every element until the iterable is exhausted.
Expand All @@ -24,7 +24,7 @@ def dropwhile(predicate, iterable, handler: ErrorHandler = None):
_initial_missing = object()


def reduce(function, sequence, initial=_initial_missing, handler: ErrorHandler = None):
def reduce(function, sequence, initial=_initial_missing, handler: Optional[ErrorHandler] = None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider giving initial a more precise type to improve type checking around reduce.

Now that handler is typed, it’d be helpful to also give initial a more precise type instead of relying on the untyped _initial_missing sentinel. For example, introduce an accumulator TypeVar and use initial: Union[_Missing, AccumulatorType] = _initial_missing (or a Sentinel protocol) so static type checkers can infer reduce’s return type more accurately and avoid Any propagation.

Suggested implementation:

def dropwhile(predicate, iterable, handler: Optional[ErrorHandler] = None):
    """
    Drop items from the iterable while predicate(item) is true.
    Afterward, return every element until the iterable is exhausted.
class _InitialMissing:
    """Sentinel type used to detect when no initial accumulator is provided."""
    __slots__ = ()


_initial_missing = _InitialMissing()

AccumulatorT = TypeVar("AccumulatorT")


def reduce(function, sequence, initial: Union[_InitialMissing, AccumulatorT] = _initial_missing, handler: Optional[ErrorHandler] = None) -> AccumulatorT:

To fully type this change you should also:

  1. Ensure the necessary typing imports are present at the top of pystreamapi/_itertools/tools.py, e.g.:
    • Add TypeVar and Union to the existing typing imports:
      from typing import Optional, TypeVar, Union
  2. Optionally, if the rest of the reduce signature is already typed elsewhere in the file, align function and sequence parameter types with existing TypeVars (e.g., Iterable[T], Callable[[AccumulatorT, T], AccumulatorT]) so that static type checkers can fully infer AccumulatorT.

"""
Apply a function of two arguments cumulatively to the items of a sequence
or iterable, from left to right, to reduce the iterable to a single
Expand Down
Loading