From 586ec161144a0a5bf5db8174d8cf7abdd161a262 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Mon, 6 Mar 2023 11:52:06 +0100 Subject: [PATCH] Handle deprecation of pkg_resources * Replace `pkg_resources` with `importlib_metadata` for `spacy info --url` * Remove requirements check from `spacy project` given the lack of alternatives --- .github/azure-steps.yml | 5 +++++ spacy/cli/info.py | 8 ++++---- spacy/cli/project/run.py | 7 ++++++- spacy/tests/test_cli.py | 1 + 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.github/azure-steps.yml b/.github/azure-steps.yml index ed69f611b..3c51e8100 100644 --- a/.github/azure-steps.yml +++ b/.github/azure-steps.yml @@ -59,6 +59,11 @@ steps: displayName: 'Test download CLI' condition: eq(variables['python_version'], '3.8') + - script: | + python -W error -m spacy info ca_core_news_sm --url + displayName: 'Test info --url CLI' + condition: eq(variables['python_version'], '3.8') + - script: | python -W error -c "import ca_core_news_sm; nlp = ca_core_news_sm.load(); doc=nlp('test')" displayName: 'Test no warnings on load (#11713)' diff --git a/spacy/cli/info.py b/spacy/cli/info.py index 974bc0f4e..1dc06d166 100644 --- a/spacy/cli/info.py +++ b/spacy/cli/info.py @@ -1,6 +1,5 @@ from typing import Optional, Dict, Any, Union, List import platform -import pkg_resources import json from pathlib import Path from wasabi import Printer, MarkdownRenderer @@ -10,6 +9,7 @@ from ._util import app, Arg, Opt, string_to_list from .download import get_model_filename, get_latest_version from .. import util from .. import about +from ..compat import importlib_metadata @app.command("info") @@ -137,10 +137,10 @@ def info_installed_model_url(model: str) -> Optional[str]: dist-info available. """ try: - dist = pkg_resources.get_distribution(model) - data = json.loads(dist.get_metadata("direct_url.json")) + dist = importlib_metadata.distribution(model) + data = dist.read_text("direct_url.json") return data["url"] - except pkg_resources.DistributionNotFound: + except importlib_metadata.PackageNotFoundError: # no such package return None except Exception: diff --git a/spacy/cli/project/run.py b/spacy/cli/project/run.py index 6dd174902..7dd0925e3 100644 --- a/spacy/cli/project/run.py +++ b/spacy/cli/project/run.py @@ -2,7 +2,6 @@ from typing import Optional, List, Dict, Sequence, Any, Iterable, Tuple import os.path from pathlib import Path -import pkg_resources from wasabi import msg from wasabi.util import locale_escape import sys @@ -331,6 +330,12 @@ def _check_requirements(requirements: List[str]) -> Tuple[bool, bool]: RETURNS (Tuple[bool, bool]): Whether (1) any packages couldn't be imported, (2) any packages with version conflicts exist. """ + msg.warn("Requirements checks are being skipped.") + return False, False + + +def _check_requirements_pkg_resources(requirements: List[str]) -> Tuple[bool, bool]: + import pkg_resources failed_pkgs_msgs: List[str] = [] conflicting_pkgs_msgs: List[str] = [] diff --git a/spacy/tests/test_cli.py b/spacy/tests/test_cli.py index dc7ce46fe..5ce8671e4 100644 --- a/spacy/tests/test_cli.py +++ b/spacy/tests/test_cli.py @@ -1126,6 +1126,7 @@ def test_cli_find_threshold(capsys): ) +@pytest.mark.skip @pytest.mark.parametrize( "reqs,output", [