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

SyntaxError does not always show line generating the error #529

Closed
bburan opened this issue Jun 7, 2023 · 3 comments
Closed

SyntaxError does not always show line generating the error #529

bburan opened this issue Jun 7, 2023 · 3 comments

Comments

@bburan
Copy link
Contributor

bburan commented Jun 7, 2023

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
Copy link
Contributor

frmdstryr commented Jun 13, 2023

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
Copy link
Contributor Author

bburan commented Jun 14, 2023

@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
Copy link
Contributor Author

bburan commented Sep 13, 2023

Closed by #530

@bburan bburan closed this as completed Sep 13, 2023
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

2 participants