Skip to content

for expressions should propagate .value of StopIteration, as map does #136177

Closed as not planned
@ReubenHillyard

Description

@ReubenHillyard

Feature or enhancement

Proposal:

map expressions propagate the .value field of StopIteration exceptions. for expressions do not. It is surprising that these two similar constructs have different behaviour in this way, and this violates the Principle of Least Surprise. for expressions should be made to also propagate .value.

def gen():
    yield 1
    yield 2
    return "Hello"

map_it = map(lambda x: x + 10, gen())
for_it = (x + 10 for x in gen())

assert next(map_it) == 11
assert next(for_it) == 11

assert next(map_it) == 12
assert next(for_it) == 12

try:
    next(map_it)
except StopIteration as exc:
    assert exc.value == "Hello"

try:
    next(for_it)
except StopIteration as exc:
    assert exc.value == None    #  current behaviour
#   assert exc.value == "Hello" # proposed behaviour

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions