-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Fix 930 pyreverse regression #984
Fix 930 pyreverse regression #984
Conversation
@Pierre-Sassoulas or @cdce8p could you please approve the workflow run? |
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.
astroid/modutils.py
Outdated
submodule_spec = importlib.util.find_spec( | ||
name + "." + modname.split(".")[0], parent_spec.submodule_search_locations | ||
modspec = importlib.machinery.PathFinder().find_spec( | ||
modname.split(".")[0], [from_file] |
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.
From what i can read in the PathFinder
doc, it seems to do the job. However i would like to have @pkolbus opinion on this. Maybe he could tell us if did not use the PathFInder
for a specific reason.
Instead of split
you can use partition
. Performance is a bit better as it doesn't split multiple times the string but just one. Moreover, as the PathFinder
doc say, there is no need to instantiate this class as all its methods are @classmethod
.
We thus could directly write:
return importlib.machinery.PathFinder.find_spec(modname.partition(".")[0], [from_file])
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.
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.
It should probably be:
return bool(
importlib.machinery.PathFinder.find_spec(
modname.split(".", maxsplit=1)[0], [from_file]
)
)
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.
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 noticed that astroid/modutils.py
seems to be missing some imports
import importlib
import importlib.machinery
astroid/modutils.py
Outdated
submodule_spec = importlib.util.find_spec( | ||
name + "." + modname.split(".")[0], parent_spec.submodule_search_locations | ||
modspec = importlib.machinery.PathFinder().find_spec( | ||
modname.split(".")[0], [from_file] |
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.
It should probably be:
return bool(
importlib.machinery.PathFinder.find_spec(
modname.split(".", maxsplit=1)[0], [from_file]
)
)
Added the imports and removed the instantiation of the PathFinder class. |
astroid/modutils.py
Outdated
submodule_spec = importlib.util.find_spec( | ||
name + "." + modname.split(".")[0], parent_spec.submodule_search_locations | ||
return bool( | ||
importlib.machinery.PathFinder.find_spec(modname.partition(".")[0], [from_file]) |
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.
importlib.machinery.PathFinder.find_spec(modname.partition(".")[0], [from_file]) | |
importlib.machinery.PathFinder.find_spec(modname.split(".", maxsplit=1)[0], [from_file]) |
We started to use split
with maxsplit=1
instead for pylint. Might be an idea to use here as well.
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.
Changed it, thanks for the suggestion.
Steps
Description
PR #857 introduced a regression for
modutils.is_relative()
.Calling this function failed if the inspected import statement was inside a module of a package at least two levels deep (e.g.
astroid.interpreter._import.spec
, see the added unit tests).Inspecting the old implementation of
is_relative
which relied onimp
and the implementation ofimp.find_module
, this only checked of the givenmodname
(or better to say the first part after splitting it on ".") was a module or package inside the same package as thefrom_file
itself.The new implementation seemed to do a lot more, and I could not really understand what exactly.
Anyways,
importlib.machinery.PathFinder().find_spec
seems to be a better replacement forimp.find_module
, makes the implementation simpler, passes all the tests andpyreverse
is working again.Type of Changes
Related Issue
Closes #930
Closes pylint-dev/pylint#4186