-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
|
||
FOLDERS_TO_IGNORE = ("node_modules", "__pycache__", "venv") | ||
|
||
HAS_REPORTED_CONDITIONAL_IMPORTS = False | ||
|
||
class BaseModule: | ||
def __init__(self, package_path, full_path): | ||
|
@@ -117,11 +118,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 commentThe 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 |
||
if not HAS_REPORTED_CONDITIONAL_IMPORTS: | ||
self._is_a_conditional_import(node) | ||
|
||
@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 commentThe reason will be displayed to describe this comment to others. Learn more. Normally |
||
for handler in node.handlers: | ||
type_info = getattr(handler, 'type', False) | ||
if type_info: | ||
exception_id = getattr(type_info, 'id', False) | ||
if exception_id == 'ImportError': | ||
print('This distribution has conditional imports') | ||
HAS_REPORTED_CONDITIONAL_IMPORTS = True | ||
Comment on lines
+135
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
break | ||
|
||
value = getattr(type_info, 'value', False) | ||
if value and value.id == 'pkg_resources' and type_info.attr == 'DistributionNotFound': | ||
print('This distribution has conditional imports') | ||
HAS_REPORTED_CONDITIONAL_IMPORTS = True | ||
break | ||
|
||
|
||
class ZCMLFile(BaseModule): | ||
"""Extract imports from .zcml files | ||
|
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?