-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: detect conditional imports #117
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not tried it, but the direction seems okay. At least it is useful to report that at least one conditional import is found.
One inline question because I don't know the code.
@@ -115,11 +116,31 @@ def _process_ast_node(self, node): | |||
file_path=self.path, | |||
is_test=self.testing, | |||
) | |||
elif isinstance(node, ast.Try): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the code. Could this condition also be true for a try/except
block that does not do any imports? Or is this part only reached for code that tries an import?
|
||
@staticmethod | ||
def _is_relative_import(import_node): | ||
return import_node.level > 0 | ||
|
||
def _is_a_conditional_import(self, node): | ||
global HAS_REPORTED_CONDITIONAL_IMPORTS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally CONSTANTS
are pretty constant and don't change. This one does, which surprised me initially.
A short comment next to the original constant would help (# Gets set to True when xyz is found
).
@@ -29,6 +29,7 @@ | |||
|
|||
FOLDERS_TO_IGNORE = ("node_modules", "__pycache__") | |||
|
|||
HAS_REPORTED_CONDITIONAL_IMPORTS = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the "reported"? It might have to do with code you'll add later. For now, it sounds like CONDITIONAL_IMPORTS_FOUND
could be clearer?
if exception_id == 'ImportError': | ||
print('This distribution has conditional imports') | ||
HAS_REPORTED_CONDITIONAL_IMPORTS = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The basic idea was to just report it, right? Just for information?
Is it easy to at a filename+linenumber at this point?
562dffa
to
94a7334
Compare
94a7334
to
61c7e0a
Compare
Closes #106
This is a rough draft, I tested with
Products.CMFPlone
and at least the code does not break 😅Wording and code organization, as well as tests are missing/can be improved.
Do you think the direction is the right one? 🤔