Skip to content

[Enhancement]: defaulted next to except StopIteration #342

@jamesbraza

Description

@jamesbraza

Overview

Firstly, hope all is well! I have successfully adopted refurb>=2 and it's working great. So excellent work with the v2 major bump.


I would like to request a new rule: reporting to go from defaulted next to except StopIteration. Please see the below Proposal.

Proposal

list_of_truthy = [False, False]

# Slightly suboptimal route 1: passing default of None
first_truthy = next((t for t in list_of_truthy if t), None)
if first_truthy is None:
    raise ValueError(f"{list_of_truthy} has no truthy items.")

# More optimal route 2: try-except
try:
    first_truthy = next(t for t in list_of_truthy if t)
except StopIteration as exc:
    raise ValueError(f"{list_of_truthy} has no truthy items.") from exc

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions