improve type hint for python runtime and generated files? #3221
Replies: 3 comments 1 reply
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
I am not hostile to the idea of enriching the existing 'abstract' classes aka 'interfaces' such that they expose methods. If you'll submit a PR (for both Python 2 and 3) I'll consider it |
Beta Was this translation helpful? Give feedback.
-
interface stubs for mypy may soon be included at typeshed. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am currently using pyright as my LSP. When I try to use the python runtime and the generated parser/lexer/visitor... files for python3, I got a lot of syntax issues.
For example, the generated files prefer to use wild card imports which pyright doesn't seem to understand.:
Possible Solution:
It looks like we are supposed to use a syntax like this in
__init__.py
instead of
we will also need to create a 'py.typed' marker file in the directory
Another issue is that the runtime uses empty classes as "interface".
Here are some of these "interfaces" in
antlr4/tree/Tree.py
:Because all of them are essentially blank/empty classes, It is impossible for a user or IDE to know what methods/properties are available on them.
Possible Solution:
We can either use stub files (.pyi) or Protocol (python 3.8)
Another issue is with 'forward declaration' which is not really supported by python.
In
antlr4/RuleContext.py
We have:
Which essentially just means
recog:None = None
to the type system.We are probably using Parser because of a circular dependency. E.g. RuleContext cann't import the actual Parser definition because the file that Parser is in is importing RuleContext.py. However, I think it might be better to just change the signature to recog = None (essentially recog:Any = None). Since we are not actually getting any benefits by typing it to 'None'
Possible solution:
we can use
from __future__ import annotations
(for 3.7+ I think) or use string.e.g.
Currently, we are dealing with textio via this line of code

which pyright doesn't understand.
However, it seems to understand it if we change it to:

Beta Was this translation helpful? Give feedback.
All reactions