Skip to content

Commit e43e045

Browse files
authored
Cache _has_init calls to avoid repeated stats (#2429)
_has_init can end up checking for the presence of the same files over and over. For example, when running pylint's import-error checks on a codebase like yt-dlp, ~43,000 redundant stats were performed prior to caching. Closes pylint-dev/pylint#9613.
1 parent 16da308 commit e43e045

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

astroid/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from astroid.modutils import (
2424
NoSourceFile,
2525
_cache_normalize_path_,
26+
_has_init,
2627
file_info_from_modpath,
2728
get_source_file,
2829
is_module_name_part_of_extension_package_whitelist,
@@ -469,6 +470,7 @@ def clear_cache(self) -> None:
469470
for lru_cache in (
470471
LookupMixIn.lookup,
471472
_cache_normalize_path_,
473+
_has_init,
472474
util.is_namespace,
473475
ObjectModel.attributes,
474476
ClassDef._metaclass_lookup_attribute,

astroid/modutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ def _is_python_file(filename: str) -> bool:
670670
return filename.endswith((".py", ".pyi", ".so", ".pyd", ".pyw"))
671671

672672

673+
@lru_cache(maxsize=1024)
673674
def _has_init(directory: str) -> str | None:
674675
"""If the given directory has a valid __init__ file, return its path,
675676
else return None.

0 commit comments

Comments
 (0)