Skip to content

Commit

Permalink
Partially work around pending deprecation of pkg_resources (explosion…
Browse files Browse the repository at this point in the history
…#12368)

* 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

* Fix installed model URL method and CI test

* Fix types/handling, simplify catch-all return

* Move imports instead of disabling requirements check

* Format

* Reenable test with ignored deprecation warning

* Fix except

* Fix return
  • Loading branch information
adrianeboyd committed Mar 9, 2023
1 parent 6f1632b commit e381efd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .github/azure-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 | grep -q download_url
displayName: 'Test download_url in info 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)'
Expand Down
17 changes: 8 additions & 9 deletions spacy/cli/info.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +9,7 @@
from .download import get_model_filename, get_latest_version
from .. import util
from .. import about
from ..compat import importlib_metadata


@app.command("info")
Expand Down Expand Up @@ -137,15 +137,14 @@ 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"))
return data["url"]
except pkg_resources.DistributionNotFound:
# no such package
return None
dist = importlib_metadata.distribution(model)
text = dist.read_text("direct_url.json")
if isinstance(text, str):
data = json.loads(text)
return data["url"]
except Exception:
# something else, like no file or invalid JSON
return None
pass
return None


def info_model_url(model: str) -> Dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion spacy/cli/project/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os.path
from pathlib import Path

import pkg_resources
from wasabi import msg
from wasabi.util import locale_escape
import sys
Expand Down Expand Up @@ -331,6 +330,7 @@ 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.
"""
import pkg_resources

failed_pkgs_msgs: List[str] = []
conflicting_pkgs_msgs: List[str] = []
Expand Down
4 changes: 3 additions & 1 deletion spacy/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import math
from collections import Counter
from typing import Tuple, List, Dict, Any
import pkg_resources
import time
from pathlib import Path

Expand Down Expand Up @@ -1126,6 +1125,7 @@ def init_nlp(
)


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize(
"reqs,output",
[
Expand Down Expand Up @@ -1158,6 +1158,8 @@ def init_nlp(
],
)
def test_project_check_requirements(reqs, output):
import pkg_resources

# excessive guard against unlikely package name
try:
pkg_resources.require("spacyunknowndoesnotexist12345")
Expand Down

0 comments on commit e381efd

Please sign in to comment.