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

Make Finder.find_module static to facilitate future caching #2509

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class Finder:
def __init__(self, path: Sequence[str] | None = None) -> None:
self._path = path or sys.path

@staticmethod
@abc.abstractmethod
def find_module(
self,
modname: str,
module_parts: Sequence[str],
processed: list[str],
Expand Down Expand Up @@ -126,8 +126,8 @@ class ImportlibFinder(Finder):
+ [(s, ModuleType.PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]
)

@staticmethod
def find_module(
self,
modname: str,
module_parts: Sequence[str],
processed: list[str],
Expand Down Expand Up @@ -221,8 +221,8 @@ def contribute_to_path(
class ExplicitNamespacePackageFinder(ImportlibFinder):
"""A finder for the explicit namespace packages."""

@staticmethod
def find_module(
self,
modname: str,
module_parts: Sequence[str],
processed: list[str],
Expand Down Expand Up @@ -261,8 +261,8 @@ def __init__(self, path: Sequence[str]) -> None:
except zipimport.ZipImportError:
continue

@staticmethod
def find_module(
self,
modname: str,
module_parts: Sequence[str],
processed: list[str],
Expand All @@ -285,8 +285,8 @@ def find_module(
class PathSpecFinder(Finder):
"""Finder based on importlib.machinery.PathFinder."""

@staticmethod
def find_module(
self,
modname: str,
module_parts: Sequence[str],
processed: list[str],
Expand Down Expand Up @@ -378,9 +378,7 @@ def _find_spec_with_path(
) -> tuple[Finder | _MetaPathFinder, ModuleSpec]:
for finder in _SPEC_FINDERS:
finder_instance = finder(search_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Micro optimization, but seems like we can now move finder_instance below the continue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two tests fail when I make that change:

FAILED tests/test_modutils.py::ModuleFileTest::test_find_egg_module - AssertionError: None != <ModuleType.PY_ZIPMODULE: 9>
FAILED tests/test_modutils.py::ModuleFileTest::test_find_zipped_module - AssertionError: None != <ModuleType.PY_ZIPMODULE: 9>

I can take a closer look in a few days.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no need then. It didn't occur to me there would be a side effect. Fine as is.

spec = finder_instance.find_module(
modname, module_parts, processed, submodule_path
)
spec = finder.find_module(modname, module_parts, processed, submodule_path)
if spec is None:
continue
return finder_instance, spec
Expand Down
Loading