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

feat: detect conditional imports #117

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

gforcada
Copy link
Collaborator

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? 🤔

@coveralls
Copy link

coveralls commented Apr 18, 2023

Coverage Status

coverage: 99.116% (-0.9%) from 99.967%
when pulling 61c7e0a on conditional-imports
into 9673df6 on master.

Copy link
Collaborator

@mauritsvanrees mauritsvanrees left a 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):
Copy link
Collaborator

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
Copy link
Owner

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
Copy link
Owner

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?

Comment on lines +133 to +137
if exception_id == 'ImportError':
print('This distribution has conditional imports')
HAS_REPORTED_CONDITIONAL_IMPORTS = True
Copy link
Owner

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?

Repository owner deleted a comment from github-actions bot Dec 28, 2023
@gforcada gforcada mentioned this pull request Feb 3, 2024
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.

Detect if imports are done within try/except blocks
4 participants