hakancelikdev / unimport

:rocket: The ultimate linter and formatter for removing unused import statements in your code.
https://unimport.hakancelik.dev/
MIT License
239 stars 22 forks source link

Check fails with match statement present in Python 3.11 #291

Closed ArneBachmannDLR closed 12 months ago

ArneBachmannDLR commented 1 year ago

Is this not yet supported?

match sort_by:
    case 'date': sort_by = ' updated DESC,'
    case _:      sort_by = ''

output:

Traceback (most recent call last):
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\libcst\_parser\base_parser.py", line 151, in _add_token
    plan = stack[-1].dfa.transitions[transition]
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: TokenType(NAME)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\bach_ar\.conda\envs\311\Scripts\unimport.exe\__main__.py", line 7, in <module>
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\unimport\__main__.py", line 6, in main
    main = Main.run()
           ^^^^^^^^^^
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\unimport\main.py", line 112, in run
    refactor_result = refactor_string(source=result.source, unused_imports=result.unused_imports)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\unimport\refactor.py", line 131, in refactor_string
    wrapper = cst.MetadataWrapper(cst.parse_module(source))
                                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\libcst\_parser\entrypoints.py", line 109, in parse_module
    result = _parse(
             ^^^^^^^
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\libcst\_parser\entrypoints.py", line 56, in _parse
    return _pure_python_parse(
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\libcst\_parser\entrypoints.py", line 89, in _pure_python_parse
    result = parser.parse()
             ^^^^^^^^^^^^^^
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\libcst\_parser\base_parser.py", line 110, in parse
    self._add_token(token)
  File "C:\Users\bach_ar\.conda\envs\311\Lib\site-packages\libcst\_parser\base_parser.py", line 186, in _add_token
    raise ParserSyntaxError(
libcst._exceptions.ParserSyntaxError: Syntax Error @ 535:9.
Incomplete input. Encountered 'sort_by', but expected ';', or 'NEWLINE'.

  match sort_by:
        ^

Hint: no error when running with --check

hakancelikdev commented 1 year ago

It looks like you are getting error during refactoring, new version of libcst may be fixing this issue, I will try to fix it.

hakancelikdev commented 1 year ago

Additionally, python 3.11 is not supported yet, but I will try to fix this issue for python 3.10.

ArneBachmannDLR commented 1 year ago

No worries, didn't know about LibCST, interesting!

hakancelikdev commented 12 months ago

It seems to be fixed, you can use it by downloading the latest version -> https://github.com/hakancelikdev/unimport/blob/98f4046426d221e17a0ca89f20e627ab09c8cdd2/tests/cases/source/statement/match_statement.py