-
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
Detect if imports are done within try/except blocks #106
Comments
At least the new one will be in the dependencies. So it is OK to report if it is missing. And if it is actually neatly in the dependencies, it won't get reported. The old one might be an old stdlib import for an old location. Or an "old-library if python < 2.6"-type dependency. If so, it would be a good idea to mention it in the readme. |
Well, the question is that, say you have the snippet from the first message buried somewhere in a module in a distribution that you don't know by heart, then you run
Without more context, and potentially with plenty of other imports around, you might simply think that both are needed as they are doing completely different things... Instead if it was reported like this:
Then it already highlights to you that before adding them on That's what's happening to me these last few days as I'm configuring plenty of random plone distributions that, by any means, I know their ins and outs 😅 |
Example is this code in
In a current PR Gil added this, because it is an outdated import:
Actually, the What I do when updating a package, if I remember to do it, is to grep for I did not yet think about it this way until now when I am writing this down, but what you are saying in that PR is: ignore A different kind of problem would be this:
In this case But it may be fine to leave it up to the user to check for |
I was worried a bit about an extra category we need to keep track of. So apart from regular imports and test imports we would also have "importerror-imports". Sounds like a big amount of work. I agree that reporting about it is a very good idea. Perhaps just collecting and printing them at the start of the report? Is that enough? |
That's what I was kind of starting to think: the user should be informed that there are imports wrapped in a try/except Thinking on it a bit more, we have this other dialect as well: try:
pkg_resources.get_distribution("Products.CMFCore")
except pkg_resources.DistributionNotFound:
# If not present, returning None suffices
def getToolByName(*args, **kw):
return None
else:
from Products.CMFCore.utils import getToolByName Which might be worth checking, like |
A slightly variation of this last example that uses if pkg_resources.get_distribution("Products.CMFCore"):
HAVE_FOO = True
else:
HAVE_FOO = False Which is sort of the same approach but does not use |
It is quite common, for backwards compatibility code, to have fallback imports, i.e.
It would be good if
z3c.dependencychecker
does not suggest that bothnew.shiny
andold.hazard
should be declared as dependencies.Not sure exactly what it should suggest though?
None feels wrong, as either one or the other should be needed to have
stuff
working down the module.Both feels way wrong, because having two imports providing the same from different packages is probably what you don't want.
Thinking a bit more, maybe mentioning them both and letting the user decide about it (i.e. not reporting it as broken) might be better, as the example above is simple, but it can be more complex, so a default option might not be desirable.
Opinions? 🍀
The text was updated successfully, but these errors were encountered: