Skip to content

Conversation

@p-sawicki
Copy link
Collaborator

mypyc may add tracebacks in the generated code with line number -1 when the AST expression that generates an error has no line set. This is usually the case for expressions that are not directly linked with a line in the Python code but are generated internally by mypyc, like helper functions generated for async functions.

These tracebacks become a problem when running a test that calls a compiled function with pytest. The line number ends up being interpreted by Python as None instead of an int and pytest crashes when trying to do math on it. So instead of a stack trace pointing to the compiled function we get an internal pytest error which makes debugging more difficult.

Changes in this PR ensure that AST nodes that may produce tracebacks have their line numbers set. For AST nodes generated by mypyc this usually means the line number of the function they are associated with, so an internal attribute access node inside a helper function generated for an async def my_fun() will have the line number of my_fun.

I didn't add a specific test for this because getting an exception from an internally generated node means that there is a bug in mypyc that we should fix but I tested it with one such error that I'm working on and pytest no longer crashes. I have also added assertions so that new changes to mypyc don't cause it to generate tracebacks with negative line numbers.

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

Successfully merging this pull request may close these issues.

1 participant