Skip to content

Commit 8628425

Browse files
authored
Cache _is_setuptools_namespace results to avoid repetitive I/O (#2705)
_is_setuptools_namespace can end up reading the same files over and over. For example, when running pylint's import-error checks on yt-dlp, ~23,500 redundant open() calls were performed prior to caching. Closes pylint-dev/pylint#9603.
1 parent 9f8faeb commit 8628425

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

astroid/interpreter/_import/spec.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def contribute_to_path(
367367
)
368368

369369

370+
@lru_cache(maxsize=1024)
370371
def _is_setuptools_namespace(location: pathlib.Path) -> bool:
371372
try:
372373
with open(location / "__init__.py", "rb") as stream:

astroid/manager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ def clear_cache(self) -> None:
439439
# pylint: disable=import-outside-toplevel
440440
from astroid.brain.helpers import register_all_brains
441441
from astroid.inference_tip import clear_inference_tip_cache
442-
from astroid.interpreter._import.spec import _find_spec
442+
from astroid.interpreter._import.spec import (
443+
_find_spec,
444+
_is_setuptools_namespace,
445+
)
443446
from astroid.interpreter.objectmodel import ObjectModel
444447
from astroid.nodes._base_nodes import LookupMixIn
445448
from astroid.nodes.scoped_nodes import ClassDef
@@ -462,6 +465,7 @@ def clear_cache(self) -> None:
462465
ObjectModel.attributes,
463466
ClassDef._metaclass_lookup_attribute,
464467
_find_spec,
468+
_is_setuptools_namespace,
465469
):
466470
lru_cache.cache_clear() # type: ignore[attr-defined]
467471

0 commit comments

Comments
 (0)