From b73457f007bd149a0f1009f58cbb3536ab9e0aac Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Sat, 11 May 2024 18:00:02 -0400 Subject: [PATCH] Cache _has_init calls to avoid repeated stats _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. --- astroid/manager.py | 2 ++ astroid/modutils.py | 1 + 2 files changed, 3 insertions(+) diff --git a/astroid/manager.py b/astroid/manager.py index fc30bf9e2f..22ceec3c29 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -23,6 +23,7 @@ from astroid.modutils import ( NoSourceFile, _cache_normalize_path_, + _has_init, file_info_from_modpath, get_source_file, is_module_name_part_of_extension_package_whitelist, @@ -457,6 +458,7 @@ def clear_cache(self) -> None: for lru_cache in ( LookupMixIn.lookup, _cache_normalize_path_, + _has_init, util.is_namespace, ObjectModel.attributes, ClassDef._metaclass_lookup_attribute, diff --git a/astroid/modutils.py b/astroid/modutils.py index 6f67d1ab9a..971e1b7297 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -666,6 +666,7 @@ def _is_python_file(filename: str) -> bool: return filename.endswith((".py", ".pyi", ".so", ".pyd", ".pyw")) +@lru_cache(maxsize=1024) def _has_init(directory: str) -> str | None: """If the given directory has a valid __init__ file, return its path, else return None.