Skip to content

🧑‍💻 Make iterator error handler optional and pin poetry-publish#124

Merged
garlontas merged 2 commits intomainfrom
bugfix/fix-codebase-problems
Apr 13, 2026
Merged

🧑‍💻 Make iterator error handler optional and pin poetry-publish#124
garlontas merged 2 commits intomainfrom
bugfix/fix-codebase-problems

Conversation

@garlontas
Copy link
Copy Markdown
Member

@garlontas garlontas commented Apr 13, 2026

Summary by Sourcery

Clarify error handler typing in iterator utilities and pin the poetry publish GitHub Action to a specific commit for publishing.

Enhancements:

  • Annotate iterator utility error handler parameters as Optional to better reflect nullable usage.

CI:

  • Pin the JRubics/poetry-publish GitHub Action in the Python publish workflow to a specific commit SHA for more deterministic releases.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 13, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR tightens type annotations in the itertools helpers by making error handler parameters explicitly Optional and pins the poetry-publish GitHub Action to a specific commit for safer, reproducible publishing.

Class diagram for updated itertools tools type annotations

classDiagram
    class tools_module {
        +dropwhile(predicate, iterable, handler Optional_ErrorHandler)
        +reduce(function, sequence, initial, handler Optional_ErrorHandler)
    }

    class ErrorHandler {
    }

    tools_module ..> ErrorHandler : uses

    class Optional_ErrorHandler {
    }

    Optional_ErrorHandler o--> ErrorHandler : wraps
Loading

File-Level Changes

Change Details Files
Make error handler arguments explicitly optional in itertools helper functions.
  • Import Optional from typing to support optional type hints.
  • Update dropwhile handler parameter type from ErrorHandler to Optional[ErrorHandler].
  • Update reduce handler parameter type from ErrorHandler to Optional[ErrorHandler].
pystreamapi/_itertools/tools.py
Pin the poetry-publish GitHub Action to a fixed commit SHA for the Python publish workflow.
  • Change the GitHub Actions workflow to use JRubics/poetry-publish by commit hash instead of a floating v2.1 tag.
.github/workflows/python-publish.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@garlontas garlontas changed the title Bugfix/fix codebase problems @sourcery-ai Apr 13, 2026
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Now that handler is explicitly Optional[ErrorHandler], consider also adding concrete type hints for predicate, iterable/sequence, and function in dropwhile and reduce to improve static checking and clarify expected call signatures.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `handler` is explicitly `Optional[ErrorHandler]`, consider also adding concrete type hints for `predicate`, `iterable`/`sequence`, and `function` in `dropwhile` and `reduce` to improve static checking and clarify expected call signatures.

## Individual Comments

### Comment 1
<location path="pystreamapi/_itertools/tools.py" line_range="27" />
<code_context>


-def reduce(function, sequence, initial=_initial_missing, handler: ErrorHandler = None):
+def reduce(function, sequence, initial=_initial_missing, handler: Optional[ErrorHandler] = None):
     """
     Apply a function of two arguments cumulatively to the items of a sequence
</code_context>
<issue_to_address>
**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:

```python
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 `TypeVar`s (e.g., `Iterable[T]`, `Callable[[AccumulatorT, T], AccumulatorT]`) so that static type checkers can fully infer `AccumulatorT`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.



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.

@sourcery-ai sourcery-ai bot changed the title @sourcery-ai 🧑‍💻 Make iterator error handler optional and pin poetry-publish Apr 13, 2026
@garlontas garlontas merged commit 18146b6 into main Apr 13, 2026
10 of 11 checks passed
@garlontas garlontas deleted the bugfix/fix-codebase-problems branch April 13, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant