Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error reporting when yapf encounters unsupported Python syntax #1194

Open
devforfu opened this issue Jan 5, 2024 · 0 comments
Open

Comments

@devforfu
Copy link

devforfu commented Jan 5, 2024

I've encountered a problem with yapf==0.32 when processing code with multiple context managers. Here is an example.

# context.py 

from contextlib import contextmanager

@contextmanager
def manager1(x):
    yield x + 1
    print("manager1 yielded")

@contextmanager
def manager2(y):
    yield y ** 2
    print("manager2 yielded")

def main():
    with (
        manager1(1) as first,
        manager2(2) as second,
    ):
        print(f"{first=}, {second=}")

if __name__ == "__main__":
    main()

See the main function. The tool doesn't like context managers separated with commas.

However, my question is not about unsupported language features, but rather about error reporting. This is what I see as the output. (Truncated to make it more readable.)

> yapf context.py
Traceback (most recent call last):
...
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=1, value='as', context=(' ', (16, 20))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=1, value='as', context=(' ', (16, 20))

During handling of the above exception, another exception occurred:
...
    return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)
IndexError: tuple index out of range

During handling of the above exception, another exception occurred:
...

    return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)
IndexError: tuple index out of range

Unfortunately, none of these lines tell you where exactly this parsing error happened.

I've stumbled upon this kind of error multiple times already, with different language features being unsupported by a version of yapf used at that time. Updating the version usually helps, but it would be nice to see files that break the tool. We use yapf as a pre-commit hook, and it might be difficult to find what exactly fails when there are multiple files changed in a pull request. Seeing the origin of the error would help to roll back to supported syntax until the tool is updated to a newer version.

@devforfu devforfu changed the title Better error reporting when yapf encounters unsupported Python syntax Improve error reporting when yapf encounters unsupported Python syntax Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant