- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.2k
 
Labels
False Positive 🦟A message is emitted but nothing is wrong with the codeA message is emitted but nothing is wrong with the code
Milestone
Description
Bug description
Within a generator expression, pylint issues use-yield-from for constructs such as:
def my_generator[T, S](iterator: Iterable[T]) -> Generator[T, S, S]:
    for item in iterator:
        state = yield iterator
    return stateWhen iterator is an Iterator and not a Generator, it will not have the generator methods (e.g. send), so applying Pylint's suggestion will modify the type of my_generator from Generator[T, S, S] to Generator[T, None, None] (and will generally cause AttributeError to be raised on calls to send).
Example file generator.py:
import typing
iterable: list[int] = [1, 2, 3]
def gen1():
    for item in iterable:
        # Implicitly suggesting that `send` will be used.
        _ = yield item
# ... and ...
# Explicitly requiring that `send` must exist.
def gen2() -> typing.Generator[int, object, None]:
    for item in iterable:
        yield itemConfiguration
No response
Command used
pylint generator.pyPylint output
************* Module generator
generator.py:6:4: R1737: Use 'yield from' directly instead of yielding each element one by one (use-yield-from)
generator.py:14:4: R1737: Use 'yield from' directly instead of yielding each element one by one (use-yield-from)Expected behavior
In many cases, I suspect that the message is worth the risk of the false positive:
def gen():
    for item in iterator:
        yield itemprobably should emit the message, but imo neither of my examples in generator.py should, maybe unless iterable could be inferred to be a compatible type (which it explicitly isn't in my example).
Pylint version
pylint 3.2.2
astroid 3.2.2
Python 3.12.0rc3 (main, Sep 21 2023, 17:27:59) [Clang 16.0.6 ]OS / Environment
No response
Additional dependencies
No response
Metadata
Metadata
Assignees
Labels
False Positive 🦟A message is emitted but nothing is wrong with the codeA message is emitted but nothing is wrong with the code