Skip to content

Commit db3385d

Browse files
committed
Ruff does not understand contextlib.suppress
Not entirely clear when F811 started triggering on this pattern (it didn't 3 months ago but is now unhappy with this). Use `find_spec` to perform a conditional import rather than recover from a failing import. This is generally less efficient, but given it's only done at import it likely doesnt matter. - successfully importing a module takes about 65ns - successfully find_spec-ing a module takes about 280ns - unsuccessfully find_spec-ing a module takes about 21*µ*s - unsuccessfully importing a modules (then recovering) takes about 26µs
1 parent 8c0f5c3 commit db3385d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/ua_parser/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"parse_user_agent",
4141
]
4242

43-
import contextlib
43+
import importlib.util
4444
from typing import Callable, Optional
4545

4646
from .basic import Resolver as BasicResolver
@@ -59,7 +59,7 @@
5959
from .loaders import load_builtins, load_lazy_builtins
6060

6161
Re2Resolver: Optional[Callable[[Matchers], Resolver]] = None
62-
with contextlib.suppress(ImportError):
62+
if importlib.util.find_spec("re2"):
6363
from .re2 import Resolver as Re2Resolver
6464

6565

0 commit comments

Comments
 (0)