Zac-HD / shed

`shed` canonicalises Python code. Shed your legacy, stop bikeshedding, and move on. Black++
https://pypi.org/project/shed/
GNU Affero General Public License v3.0
342 stars 23 forks source link

[BUG] `Could not parse file` Error on Pattern Matching in 3.10 #46

Closed OtherBarry closed 2 years ago

OtherBarry commented 2 years ago

When I run shed in a repo that uses pattern matching, I get the following error:

Could not parse file './some/file/with/pattern/matching'
    The syntax is valid Python, so please report this as a bug.

Let me know if there's anything I can do to assist.

Zac-HD commented 2 years ago

This doesn't seem to trigger on the examples in PEP 636, which I added to Shed's test suite in #47.

It would be very helpful if you could upgrade to that version, check that it still occurs, and then provide a file which triggers this problem! Otherwise I'm stuck in "can't reproduce 😢" territory, and can't do much. Minimal examples preferred, but if you want to just dump the whole original file that's fine too.

OtherBarry commented 2 years ago

Still getting the issue.

Created a fresh virtualenv and ran pip install shed. Pip freeze now outputs this:

autoflake==1.5.3
black==22.8.0
click==8.1.3
com2ann==0.3.0
isort==5.10.1
libcst==0.4.7
mypy-extensions==0.4.3
pathspec==0.10.1
platformdirs==2.5.2
pyflakes==2.5.0
pyupgrade==2.37.3
PyYAML==6.0
shed==0.10.2
tokenize-rt==4.2.1
toml==0.10.2
tomli==2.0.1
typing-inspect==0.8.0
typing_extensions==4.3.0

I created a file named shed_test.py, with the following content

import random

choice = random.choice(["foo", "bar", "baz", "error"])

match choice:
    case "foo":
        print("foo")
    case "bar":
        print("bar")
    case "baz":
        print("baz")
    case _:
        print("Something went wrong")

It runs as expected with python 3.10.4. When I run shed --py310-plus shed_test.py, I get the following error:

Internal error formatting 'shed_test.py': InvalidInput: Cannot parse: 5:6: match choice:
    Please report this to https://github.com/Zac-HD/shed/issues
Zac-HD commented 2 years ago

OK, thanks for the repro! I've got a partial fix to clean up and release later this week - I had a bug which wasn't enforcing the lower bound, which would have been fine except that Black doesn't detect that "match statement implies 3.10+".

I'll also need to fix the way versions are configured for LibCST (for --refactor mode), since apparently their transition to Rust added support for pattern matching but didn't list 3.9 or later in the known versions 😕

Zac-HD commented 2 years ago

Fixed in 0.10.3 / https://github.com/Zac-HD/shed/commit/6abc9b62de9e788d54cfaf25228e3310c18353be 🙂

OtherBarry commented 2 years ago

Thanks, works great!