Skip to content

Non-None return value from generator should throw RuntimeError #127306

@jvm-pangea

Description

@jvm-pangea

Bug report

Bug description:

def y1(*args):
    return args

def y2(*args):
    return args
    yield 3

print("y1: " + str(list(y1(1, 2, 3, 4))))
print("y2: " + str(list(y2(1, 2, 3, 4))))

While the yield in y2 is unreachable (this happened to me while re-writing a function and leaving what I thought was dead code at the end), y2 is considered a Generator, and iterating over it produces an immediate StopIteration error with args as the argument, which manifests as an empty iterator.

I can't imagine a good reason to create a StopIteration exception with a non-None argument. With a None argument, this signals the basic case where a Generator is returning an empty iterator.

Consider throwing RuntimeError when StopIteration is raised with a non-None valued error when calling next to prevent this confusing behaviour.

CPython versions tested on:

3.10

Operating systems tested on:

macOS

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