This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: lib2to3 fails on a slash('/') after positional_only paramter in a function signature
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.8
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, cloud-yu, lukasz.langa, xtreak
Priority: normal Keywords:

Created on 2021-04-30 09:13 by cloud-yu, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg392396 - (view) Author: MisterY (cloud-yu) Date: 2021-04-30 09:13
When I try to format a code snippet with yapf, there comes a error.
It seems lib2to3 can't reconised SLASH.

echo "def foo(posonly1, posonly2, /, positional_or_keyword): pass" | yapf 

Traceback (most recent call last):
  File "d:\program files\python38\lib\site-packages\yapf\yapflib\pytree_utils.py", line 115, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "d:\program files\python38\lib\lib2to3\pgen2\driver.py", line 103, in parse_string
    return self.parse_tokens(tokens, debug)
  File "d:\program files\python38\lib\lib2to3\pgen2\driver.py", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "d:\program files\python38\lib\lib2to3\pgen2\parse.py", line 162, in addtoken
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=17, value='/', context=(' ', (1, 28))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\program files\python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\program files\python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\Program Files\Python38\Scripts\yapf.exe\__main__.py", line 7, in <module>
  File "d:\program files\python38\lib\site-packages\yapf\__init__.py", line 362, in run_main
    sys.exit(main(sys.argv))
  File "d:\program files\python38\lib\site-packages\yapf\__init__.py", line 104, in main
    reformatted_source, _ = yapf_api.FormatCode(
  File "d:\program files\python38\lib\site-packages\yapf\yapflib\yapf_api.py", line 147, in FormatCode
    tree = pytree_utils.ParseCodeToTree(unformatted_source)
  File "d:\program files\python38\lib\site-packages\yapf\yapflib\pytree_utils.py", line 121, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "d:\program files\python38\lib\lib2to3\pgen2\driver.py", line 103, in parse_string
    return self.parse_tokens(tokens, debug)
  File "d:\program files\python38\lib\lib2to3\pgen2\driver.py", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "d:\program files\python38\lib\lib2to3\pgen2\parse.py", line 162, in addtoken
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=17, value='/', context=(' ', (1, 28))
msg392397 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-04-30 09:18
The slash syntax was introduced in 3.8. I guess the grammar of lib2to3 was not updated to support new syntax elements.

Łukasz, could you please take a look?
msg392398 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-04-30 09:33
https://bugs.python.org/issue36541 add support for walrus operator and positional arguments. Relevant commit : https://github.com/python/cpython/commit/06bfd033e847bedb6e123d131dcf46393a4555df

Relevant yapf issue on python 3.8 support : https://github.com/google/yapf/issues/772

Can you please add the exact python version you are using?
msg392399 - (view) Author: MisterY (cloud-yu) Date: 2021-04-30 09:36
python 3.8.3
msg392400 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-04-30 09:40
Thanks, the change was merged in December 14, 2020 just missing 3.8.7 . Can you please upgrade to 3.8.8 or 3.8.9 and try? If the issue still persists I guess it's then an issue with yapf since Python 3.8.8 has the code merged with tests.
msg392694 - (view) Author: MisterY (cloud-yu) Date: 2021-05-02 13:23
Thanks, it works well after upgrade to version 3.8.9
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88151
2021-05-02 13:23:21cloud-yusetstatus: open -> closed

messages: + msg392694
stage: resolved
2021-04-30 09:40:12xtreaksetmessages: + msg392400
2021-04-30 09:36:14cloud-yusetmessages: + msg392399
2021-04-30 09:33:41xtreaksetnosy: + xtreak
messages: + msg392398
2021-04-30 09:18:55christian.heimessetnosy: + christian.heimes, lukasz.langa
messages: + msg392397
2021-04-30 09:13:19cloud-yucreate