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

Py runtime: Move to relative imports #4705

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

pelson
Copy link

@pelson pelson commented Sep 29, 2024

This also fixes a few from antlr4 import *, which is considered bad practice unless being done to expose an interface (https://peps.python.org/pep-0008/#imports)

My changes seem to modify generated files in antlr4.xpath. I didn't yet look at fixing the generator.

There remains some significant issues withe the namespaces being exposed in the runtime - I would be happy to follow up after this change to tighten the names being made publicly available.

@@ -52,7 +52,7 @@ def removeErrorListeners(self):
def getTokenTypeMap(self):
tokenNames = self.getTokenNames()
if tokenNames is None:
from antlr4.error.Errors import UnsupportedOperationException
from .error.Errors import UnsupportedOperationException
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that the reason these imports were made lazy was because of circular import issues... This MR should start to straighten that out.

from antlr4.error.ErrorStrategy import BailErrorStrategy
from antlr4.error.DiagnosticErrorListener import DiagnosticErrorListener
from antlr4.Utils import str_list
from .Token import Token
Copy link
Author

@pelson pelson Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future I would recommend making these explicit export based imports, as mentioned in https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-implicit-reexport.

I didn't do that here because I would try to add some validation of this expectation.

@ericvergnaud
Copy link
Contributor

@pelson thanks for this. LGTM apart from my 2 comments.

@pelson
Copy link
Author

pelson commented Sep 30, 2024

Thanks @ericvergnaud - I think you forgot to hit submit review. Comments missing.

import importlib
from antlr4 import *

from . import *
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place where import * is not fixed - I would be happy to follow-up in another PR to make the names being used explicit.

from antlr4.error.Errors import LexerNoViableAltException
from antlr4.tree.Tree import ParseTree
from antlr4.tree.Trees import Trees
from ..Lexer import Lexer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is generated, and generated files cannot use relative imports since they normally sit outside the antlr code base. Please revert.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is XPath.py really auto-generated? I don't see it in https://github.com/antlr/antlr4/blob/dev/doc/releasing-antlr.md#build-xpath-parsers (unlike XPathLexer.py which definitely is auto-generated)

@@ -1,5 +1,11 @@
# Generated from XPathLexer.g4 by ANTLR 4.13.1
from antlr4 import *
from ..Lexer import Lexer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is generated, and generated files cannot use relative imports since they normally sit outside the antlr code base. Please revert

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted in my update

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.

2 participants