cpython
cpython copied to clipboard
Incorrect error message for `yield from`
Bug report
Bug description:
% ./python.exe -c 'yield x'
File "<string>", line 1
SyntaxError: 'yield' outside function
% ./python.exe -c 'yield from x'
File "<string>", line 1
SyntaxError: 'yield' outside function
% ./python.exe -c 'async def f(): yield from x'
File "<string>", line 1
SyntaxError: 'yield from' inside async function
The middle one should also say "yield from".
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-121680
- gh-121722
- gh-121768
- gh-121769
This is very easy to fix but I'll leave it for a bit in case a new contributor wants to get familiar with making a CPython change. Pointers for the fix:
- The code to be changed is in
compile.c, currently at line 6122 (search for "'yield' outside function" if it changes). - Tests should be added, probably around here
Lib/test/test_generators.py:SyntaxError: 'yield' outside function. The existing test only testsyield, notyield from; we should add more tests.
I am not sure 5his should be fixed, as technically the error message is correct.
The error message in the third example is more specific because yield is valid here.
I will give it a try :)
The existing message was technically correct, but yield from is definitely better.