You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: Since I am unfamiliar with pull requests and testing, would someone please test and update the release code with the proposed two changes given below. Thanks.
Edit: there are actually two bugs.
Parsing the input file uses the Python 2 grammar even though the input file is a Python 3 file.
The problem is with a print() call. The word print is a keyword which must be at the start of a statement.
In most situations this is not a problem, because print(x, y, z) appears as a complete expression statement. The parser interprets this as printing with (x, y, z) as a tuple to be printed.
However, merge_pyi crashes when print appears in some other way, including, for example:
print(x, file=f)(this happened to me)
lambda x: print(x)
func(print)
Cause of the bug
The parse is done by the RefactoringTool.refactor_string() method in lib2to3.refactor. This object is constructed with possible options, including "print_function". In order to treat the string in Python 3 syntax, it is necessary for this option to have a true value instead of the default False. pytype.tools.merge_pyi.main()does not do this.
Proposed fix
Simply pass the needed option to RefactoringTool.refactor_string(). No need to worry about a Python 2.7 file being parsed, because pytype -V2.7 is not supported. N.B.: You might want to be sure that the documentation is clear that Python 3 files are required for all of the tools, and the Python version must be an acceptable value for pytype -V.
Change merge_pyi.py line 923 to read:
class StandaloneRefactoringTool(refactor.RefactoringTool):
"""Modified RefactoringTool for running outside the standard 2to3 install."""
def __init__(self, options):
self._fixer = None
super().__init__([], options=options)
...
...
Second bug
The first bug (above) applies only to the .py input. The second bug applies similarly to the .pyi input.
In my case, I had a class which has a print() method. The parser didn't parse the def print() -> None statement, as it's expecting Python2 syntax.
Note: Since I am unfamiliar with pull requests and testing, would someone please test and update the release code with the proposed two changes given below. Thanks.
Edit: there are actually two bugs.
Parsing the input file uses the Python 2 grammar even though the input file is a Python 3 file.
The problem is with a
print()
call. The wordprint
is a keyword which must be at the start of a statement.In most situations this is not a problem, because
print(x, y, z)
appears as a complete expression statement. The parser interprets this as printing with(x, y, z)
as a tuple to be printed.However, merge_pyi crashes when
print
appears in some other way, including, for example:print(x, file=f)
(this happened to me)lambda x: print(x)
func(print)
Cause of the bug
The parse is done by the
RefactoringTool.refactor_string()
method inlib2to3.refactor
. This object is constructed with possible options, including"print_function"
. In order to treat the string in Python 3 syntax, it is necessary for this option to have a true value instead of the defaultFalse
.pytype.tools.merge_pyi.main()
does not do this.Proposed fix
Simply pass the needed option to
RefactoringTool.refactor_string()
. No need to worry about a Python 2.7 file being parsed, becausepytype -V2.7
is not supported.N.B.: You might want to be sure that the documentation is clear that Python 3 files are required for all of the tools, and the Python version must be an acceptable value for
pytype -V
.Change
merge_pyi.py
line 923 to read:instead of
Note that the code does not need changing for:
Second bug
The first bug (above) applies only to the
.py
input. The second bug applies similarly to the.pyi
input.In my case, I had a class which has a
print()
method. The parser didn't parse thedef print() -> None
statement, as it's expecting Python2 syntax.Proposed fix
Change
merge_pyi.py
line 102 fromto
I made this change in my own installed package, and the tool works correctly now with the cases where it had failed.
The text was updated successfully, but these errors were encountered: