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

Antlr4 (target Python 3) generated files and mypy (>= 0.91) type-checking issues #4183

Open
1 task
DagSonntag opened this issue Mar 15, 2023 · 3 comments
Open
1 task

Comments

@DagSonntag
Copy link

DagSonntag commented Mar 15, 2023

Hi
I have been using mypy together with Antlr4 python generated code for some time. This has mainly worked since I have been able to exclude any Antlr4 generated files from the mypy-check since they have never been fully compliant. However, lately, from mypy 0.91 forward, some errors are raised even for files that are excluded in case it affects files dependent on the excluded files. This, in combination with that the Antlr generated files are massive, creates an issue since the errors from those files bloats the log of any mypy type-checking, making it hard to see the errors in ones own code.

This is certainly an issue for mypy, but I am also wondering if it is desirable to make Antrl4 python generated code more mypy (PEP) typing-friendly?
The main issues I have found are (examples from the Java/Java grammar):

  • The usage of * imports.
    - In for example JavaParser the first import statement is from antlr4 import *. This is something mypy does not like, and results in a lot of 'name-defined' errors like JavaParser.py:11145: error: Name "ParseTreeListener" is not defined [name-defined]
    - Could be resolved by explicitly importing the classes instead, replacing the *

(Edit: Two additional issues were also raised previously, but are now moved to separate issues (#4188 #4189 ))

So, my question is. Would these fixes be desirable or have there been some discussions to what level/PEP the generated files should be follow? (i.e. Is this seen as an issue or will provided fixes be rejected)

Last of all; Thank you for a great tool! It is most useful! :)

@ericvergnaud
Copy link
Contributor

ericvergnaud commented Mar 15, 2023 via email

@DagSonntag
Copy link
Author

DagSonntag commented Mar 16, 2023

Hi
Thank you for the fast response.
Hmm, ok, I haven't looked into the code yet, but there would seem to me like there are two ways to do it

  • Either have every generated method return what imports it is using
  • Look through the document for certain keywords (imports) at the end

Then do a post-edit of the first lines of the generated document/file. You don't think any of those strategies are possible with the current code generation flow? Or do the lines in the file have to be generated in order?

@ericvergnaud
Copy link
Contributor

Hi,
Sorry for the late reply
I think both of these would be very costly in code changes, and thus risky.

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