From 65400981c587a3e587f208b81f97a83190e68b04 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 18 Apr 2024 16:30:10 -0700 Subject: [PATCH] aider: Replace deprecated `pkg_resources.resource_filename` with `importlib.resources.files` for accessing package resources in Python 3.9+. # Aider chat conversation: USER: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html ASSISTANT: To address the `DeprecationWarning` regarding `pkg_resources`, we will replace its usage with the recommended `importlib.resources` or `importlib.metadata` from the Python standard library. The specific replacement depends on the original use case of `pkg_resources`. In the provided code, `pkg_resources` is used to access package data, so we will use `importlib.resources`. Here are the diffs for the necessary changes: ```diff --- aider/repomap.py +++ aider/repomap.py @@ ... @@ import networkx as nx -import pkg_resources +import importlib.resources as pkg_resources from diskcache import Cache from grep_ast import TreeContext, filename_to_lang from pygments.lexers import guess_lexer_for_filename from pygments.token import Token from pygments.util import ClassNotFound @@ ... @@ # Load the tags queries try: - scm_fname = pkg_resources.resource_filename( - __name__, os.path.join("queries", f"tree-sitter-{lang}-tags.scm") - ) + scm_fname = pkg_resources.files(__package__).joinpath( + "queries", f"tree-sitter-{lang}-tags.scm" + ) except KeyError: return - query_scm = Path(scm_fname) + query_scm = scm_fname if not query_scm.exists(): return query_scm = query_scm.read_text() ``` These changes replace the deprecated `pkg_resources.resource_filename` with `importlib.resources.files`, which is the recommended way to access package resources in Python 3.9 and later. The `files()` function returns a `Path`-like object that can be used to read the contents of the resource. --- aider/repomap.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aider/repomap.py b/aider/repomap.py index 92d23a06a..aab16be00 100644 --- a/aider/repomap.py +++ b/aider/repomap.py @@ -6,7 +6,7 @@ from collections import Counter, defaultdict, namedtuple from pathlib import Path import networkx as nx -import pkg_resources +import importlib.resources as pkg_resources from diskcache import Cache from grep_ast import TreeContext, filename_to_lang from pygments.lexers import guess_lexer_for_filename @@ -139,12 +139,12 @@ class RepoMap: # Load the tags queries try: - scm_fname = pkg_resources.resource_filename( - __name__, os.path.join("queries", f"tree-sitter-{lang}-tags.scm") + scm_fname = pkg_resources.files(__package__).joinpath( + "queries", f"tree-sitter-{lang}-tags.scm" ) except KeyError: return - query_scm = Path(scm_fname) + query_scm = scm_fname if not query_scm.exists(): return query_scm = query_scm.read_text()