Skip to content

new rule - re-iteration of generator that's already been iterated over #4503

Description

@DetachHead

to my knowledge no existing linter or type checker is able to detect mistakes like this. for example:

def get_values() -> list[int]:
    return [i for i in range(3)]

values = get_values()

assert 4 not in values # passes
assert 1 in values # passes

but if get_values is updated to return a Generator instead of a list, it can unintentionally break existing usages:

from typing import Generator

def get_values() -> Generator[int]:
    return (i for i in range(3))

values = get_values()

assert 4 not in values # passes
assert 1 in values # fails because the generator has no more values to iterate

i've encountered issues like this several times, so it would be nice if ty had a rule for it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lintLabel for features that we would implement as lint rules, not core type checker featureswishNot on the current roadmap; maybe in the future

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions