nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.52k stars 130 forks source link

SyntaxError does not always show line generating the error #529

Closed bburan closed 10 months ago

bburan commented 1 year ago

For some reason SyntaxError sometimes shows SyntaxError: invalid syntax (foo.enaml) rather than a more detailed error message. I am unable to track down the cause because I see in the unit tests that it should work. I ran core/parser/test_parser.py::test_syntax_error_traceback_correct_path and although the unit test fails on Python 3.11, it actually is generating the correct traceback, e.g:

  File "C:\Users\mmm\AppData\Local\Temp\pytest-of-mmm\pytest-12\test_syntax_error_traceback_co0\view.enaml", line (5, 35)
    Label # : missing intentionally
                                  ^
SyntaxError: invalid syntax

However, if I create a foo.enaml file:

from enaml.widgets.api import Container, Label

enamldf Foo:
    pass

Then, enaml-run just prints out:

  File "C:\Users\mmm\miniconda3\envs\psi4\Lib\site-packages\enaml\core\parser\base_python_parser.py", line 85, in parse
    raise SyntaxError("invalid syntax", (self.filename, token.start, offset, token.line))
SyntaxError: invalid syntax (foo.enaml)

Running it a different way, e.g., via python test_foo.py containing the following:

import enaml
with enaml.imports():
    import foo

Gives the same error (no information about what generated the error). I'm pretty stumped. I suspect this may be something related to Python 3.11 as the above code works fine on 3.10.

I dug into the base_python_parser.py code and I see if we update the code to match the new SyntaxError format where lineno, offset, end_lineno, end_offset are all provided, it generates a more informative error.

  File "C:\Users\mmm\miniconda3\envs\psi4\Lib\site-packages\enaml\core\parser\base_python_parser.py", line 83, in parse
    raise SyntaxError("invalid syntax", (self.filename, lineno, offset, token.line, end_lineno, end_offset))
  File "foo.enaml", line 4
    enamldf Foo:

I've submitted this patch, but I cannot figure out how to get a unit-test working to detect this. I think it may have to do with how Enaml imports the module?

frmdstryr commented 1 year ago

Seems like with the new parser indentation errors also do not provide the line or any context (like which file).

from enaml.widgets.api import Window, Container, Label

enamldef Main(Window): window:
    Container:
        Label:
        text = "Hello!"

Just gives

# large traceback ommitted
  File "/home/user/projects/enaml/enaml/core/parser/base_python_parser.py", line 104, in raise_indentation_error
    raise IndentationError(msg)
IndentationError: expected an indented block

Your PR works well for syntax errors! Thanks!!

bburan commented 1 year ago

@frmdstryr I think IndentationError is a subclass of SyntaxError, which makes me wonder if it would be as simple as providing the same argument tuple as given to SyntaxError?

bburan commented 10 months ago

Closed by #530