Skip to content

Commit

Permalink
Modify error message if certifi is not installed (#3402)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Dec 17, 2024
1 parent 71dff09 commit 7e3dd15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/scanpy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ def old_positionals(*old_positionals: str):
return lambda func: func


if sys.version_info >= (3, 11):

@wraps(BaseException.add_note)
def add_note(exc: BaseException, note: str) -> None:
exc.add_note(note)
else:

def add_note(exc: BaseException, note: str) -> None:
if not hasattr(exc, "__notes__"):
exc.__notes__ = []
exc.__notes__.append(note)


if sys.version_info >= (3, 13):
from warnings import deprecated as _deprecated
else:
Expand Down
23 changes: 11 additions & 12 deletions src/scanpy/readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from matplotlib.image import imread

from . import logging as logg
from ._compat import old_positionals
from ._compat import add_note, old_positionals
from ._settings import settings
from ._utils import _empty

Expand Down Expand Up @@ -993,15 +993,11 @@ def _get_filename_from_key(key, ext=None) -> Path:


def _download(url: str, path: Path):
try:
import ipywidgets # noqa: F401
from tqdm.auto import tqdm
except ImportError:
from tqdm import tqdm

from urllib.error import URLError
from urllib.request import Request, urlopen

from tqdm.auto import tqdm

blocksize = 1024 * 8
blocknum = 0

Expand All @@ -1011,14 +1007,17 @@ def _download(url: str, path: Path):
try:
open_url = urlopen(req)
except URLError:
logg.warning(
"Failed to open the url with default certificates, trying with certifi."
)
msg = "Failed to open the url with default certificates."
try:
from certifi import where
except ImportError as e:
add_note(e, f"{msg} Please install `certifi` and try again.")
raise
else:
logg.warning(f"{msg} Trying to use certifi.")

from ssl import create_default_context

from certifi import where

open_url = urlopen(req, context=create_default_context(cafile=where()))

with open_url as resp:
Expand Down

0 comments on commit 7e3dd15

Please sign in to comment.