Open MichaReiser opened 4 days ago
def get_file_paths(file_types: Iterable[str] | None = None) -> Iterable[Path]: dir_path = Path(".") if file_types is None: return dir_path.glob("*") for file_type in file_types: (yield a for a in dir_path.glob(f"*.{file_type}"))
hmm, this isn't valid syntax:
>>> from typing import *
>>> from pathlib import Path
>>> def get_file_paths(file_types: Iterable[str] | None = None) -> Iterable[Path]:
... dir_path = Path(".")
... if file_types is None:
... return dir_path.glob("*")
...
... for file_type in file_types:
... (yield a for a in dir_path.glob(f"*.{file_type}"))
...
File "<python-input-2>", line 7
(yield a for a in dir_path.glob(f"*.{file_type}"))
^^^
SyntaxError: invalid syntax
hmm, I only tested with our parser. nested yield expressions are definitely valid :)
Okay, found one
async def main():
await (yield x)
Here's another function that is a generator function with a return
statement and is not flagged by B901:
def f():
x = yield
print(x)
return 42
This is a generator function that you can send
values into:
>>> def f():
... x = yield
... print(x)
... return 42
...
>>> y = f()
>>> next(y)
>>> y.send('foo')
foo
Traceback (most recent call last):
File "<python-input-13>", line 1, in <module>
y.send('foo')
~~~~~~^^^^^^^
StopIteration: 42
Send
ing valued into a generator is, like return
ing values from a generator, quite an advanced feature. So if I saw a function like this in code review, I would assume the user knew what they were doing. That doesn't feel like a very principled reason not to emit B901 on it, though; maybe we should. Not sure.
Hi @MichaReiser @AlexWaygood can I help you fix this issue ?
Sure. You can find the code for this rule in this file
Thanks @MichaReiser excited to work on this !
A very contrived example but
B901
misses yield expressions where they're not the expression of aExprStmt
.Note: we should make sure that we don't traverse into any lambda expressions...