hauntsaninja / no_implicit_optional

A codemod to make your implicit optional type hints PEP 484 compliant.
MIT License
79 stars 8 forks source link

Unable to parse compound with block #8

Closed kojiromike closed 1 year ago

kojiromike commented 1 year ago

Python 3.9 and up can parse a compound with block:

$ cat test.py 
from io import StringIO

with (StringIO() as a, StringIO() as b):
    a.write("I am a")
    b.write("I am b")
    print(a.getvalue())
    print(b.getvalue())

$ python3.9 -m test
I am a
I am b

But even when using python3.11 to run no_implicit_optional, it errors on that kind of expression.

$ python3.11 -m no_implicit_optional test.py 
Calculating full-repo metadata...
Executing codemod...
Codemodding /Users/michaelsmith/test.py
Traceback (most recent call last):
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/base_parser.py", line 151, in _add_token
    plan = stack[-1].dfa.transitions[transition]
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: ReservedString(as)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/codemod/_cli.py", line 271, in _execute_transform
    input_tree = parse_module(
                 ^^^^^^^^^^^^^
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 109, in parse_module
    result = _parse(
             ^^^^^^^
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 56, in _parse
    return _pure_python_parse(
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 89, in _pure_python_parse
    result = parser.parse()
             ^^^^^^^^^^^^^^
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/base_parser.py", line 110, in parse
    self._add_token(token)
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/base_parser.py", line 186, in _add_token
    raise ParserSyntaxError(
libcst._exceptions.ParserSyntaxError: Syntax Error @ 3:18.
Incomplete input. Encountered 'as', but expected ')'.

with (StringIO() as a, StringIO() as b):
                 ^

Failed to codemod /Users/michaelsmith/test.py

Finished codemodding 1 files!
 - Transformed 0 files successfully.
 - Skipped 0 files.
 - Failed to codemod 1 files.
 - 0 warnings were generated.
hauntsaninja commented 1 year ago

This is a shortcoming of libcst, the library we use to obtain a concrete syntax tree. Could you try with setting export LIBCST_PARSER_TYPE=native?

kojiromike commented 1 year ago

Interesting. It errors at a different spot. This is consistent with 3.9, 10 and 11.

$ LIBCST_PARSER_TYPE=native python3.11 -m no_implicit_optional test.py
Calculating full-repo metadata...
Executing codemod...
Codemodding /Users/michaelsmith/test.py
Traceback (most recent call last):
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/codemod/_cli.py", line 271, in _execute_transform
    input_tree = parse_module(
                 ^^^^^^^^^^^^^
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 109, in parse_module
    result = _parse(
             ^^^^^^^
  File "/Users/michaelsmith/.pyenv/versions/3.11.0/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 55, in _parse
    return parse(source_str)
           ^^^^^^^^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 4:22.
parser error: error at 3:38: expected *

    a.write("I am a")
                     ^

Failed to codemod /Users/michaelsmith/test.py

Finished codemodding 1 files!
 - Transformed 0 files successfully.
 - Skipped 0 files.
 - Failed to codemod 1 files.
 - 0 warnings were generated.
hauntsaninja commented 1 year ago

Hmm, not sure about that. If you can create a version that's shareable, I'd report over at https://github.com/Instagram/LibCST/issues Not much I can do within this repo, unfortunately.