From 08318e5202afa74e3025ec429545b63f364c648d Mon Sep 17 00:00:00 2001 From: valentino-sm <5059499+valentino-sm@users.noreply.github.com> Date: Thu, 16 Nov 2023 22:18:02 +0500 Subject: [PATCH] Fix DeprecationWarning: pkg_resources is deprecated as an API (#19) --- pymorphy2/analyzer.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pymorphy2/analyzer.py b/pymorphy2/analyzer.py index f4686e8..208d36e 100644 --- a/pymorphy2/analyzer.py +++ b/pymorphy2/analyzer.py @@ -102,18 +102,25 @@ def apply_to_tags(self, word, word_lower, tags): def _iter_entry_points(*args, **kwargs): - """ Like pkg_resources.iter_entry_points, but uses a WorkingSet which - is not populated at startup. This ensures that all entry points - are picked up, even if a package which provides them is installed + """ + Finds entry points of installed packages, including ones installed after the current process is started. - The main use case is to make ``!pip install pymorphy2`` work + The main use case is to make ``!pip install pymorphy2-fork`` work within a Jupyter or Google Colab notebook. See https://github.com/kmike/pymorphy2/issues/131 """ - import pkg_resources - ws = pkg_resources.WorkingSet() - return ws.iter_entry_points(*args, **kwargs) + from importlib.metadata import entry_points, EntryPoint + ep = entry_points() + result: set[EntryPoint] = set() + if not hasattr(ep, 'select'): + # Python < 3.10 old entry_points API + for group in args: + result.update(ep.get(group, ())) + else: + for group in args: + result.update(ep.select(group=group)) + return result def _lang_dict_paths():