Skip to content

Commit

Permalink
Ruff does not understand contextlib.suppress
Browse files Browse the repository at this point in the history
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
  • Loading branch information
masklinn committed Jul 13, 2024
1 parent 8c0f5c3 commit 24a1190
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ua_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"parse_user_agent",
]

import contextlib
import importlib.util
from typing import Callable, Optional

from .basic import Resolver as BasicResolver
Expand All @@ -59,7 +59,7 @@
from .loaders import load_builtins, load_lazy_builtins

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


Expand Down

0 comments on commit 24a1190

Please sign in to comment.