Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust opcode line numbers for return statements in python 3.10+
Python 3.10 changed the way line numbers are assigned to `return` statements within `with` blocks, to attach to the `with` statement. This follows the actual control flow in the python interpreter, but makes it hard to report errors attached to the right line in the source code. We examine the AST and use the line number information from the parser to adjust the `RETURN_VALUE` opcodes to have the same line numbers as the source `return` statements that generated them. `RETURN_VALUE` opcodes corresponding to implicit `return None` continue to be moved to the last line of the function. PiperOrigin-RevId: 460639960
- Loading branch information
1 parent
ebf1d16
commit 65fb497
Showing
6 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Tests for bad-return-type errors.""" | ||
|
||
from pytype.tests import test_base | ||
|
||
|
||
class TestReturns(test_base.BaseTest): | ||
"""Tests for bad-return-type.""" | ||
|
||
def test_implicit_none(self): | ||
self.CheckWithErrors(""" | ||
def f(x) -> int: | ||
pass # bad-return-type | ||
""") | ||
|
||
def test_if(self): | ||
# NOTE(b/233047104): The implict `return None` gets reported at the end of | ||
# the function even though there is also a correct return on that line. | ||
self.CheckWithErrors(""" | ||
def f(x) -> int: | ||
if x: | ||
pass | ||
else: | ||
return 10 # bad-return-type | ||
""") | ||
|
||
def test_nested_if(self): | ||
self.CheckWithErrors(""" | ||
def f(x) -> int: | ||
if x: | ||
if __random__: | ||
pass | ||
else: | ||
return 'a' # bad-return-type | ||
else: | ||
return 10 | ||
pass # bad-return-type | ||
""") | ||
|
||
def test_with(self): | ||
self.CheckWithErrors(""" | ||
def f(x) -> int: | ||
with open('foo'): | ||
if __random__: | ||
pass | ||
else: | ||
return 'a' # bad-return-type # bad-return-type | ||
""") | ||
|
||
def test_nested_with(self): | ||
self.CheckWithErrors(""" | ||
def f(x) -> int: | ||
with open('foo'): | ||
if __random__: | ||
with open('bar'): | ||
if __random__: | ||
pass | ||
else: | ||
return 'a' # bad-return-type # bad-return-type | ||
""") | ||
|
||
|
||
if __name__ == "__main__": | ||
test_base.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters