Skip to content

Commit

Permalink
Fix importlib(_|.)metadata handling for various python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
deathowl committed Apr 11, 2023
1 parent dee8c39 commit 94c1b45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@
)
from typing_extensions import Any as SubclassableAny

if sys.version_info >= (3, 10):
if sys.version_info < (3,8):
from importlib_metadata import entry_points
else:
from importlib.metadata import entry_points

if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
from importlib_metadata import entry_points
from typing_extensions import ParamSpec

TypeCheckerCallable: TypeAlias = Callable[
Expand Down Expand Up @@ -847,7 +850,12 @@ def load_plugins() -> None:
``TYPEGUARD_DISABLE_PLUGIN_AUTOLOAD`` environment variable is present.
"""

for ep in entry_points(group="typeguard.checker_lookup"):
group = "typeguard.checker_lookup"
if sys.version_info >= (3, 10):
eps = entry_points(group=group)
else:
eps = entry_points().get(group, ())
for ep in eps:
try:
plugin = ep.load()
except Exception as exc:
Expand Down
13 changes: 9 additions & 4 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from pytest import MonkeyPatch

from typeguard import load_plugins
Expand All @@ -12,10 +14,13 @@ class FakeEntryPoint:

def load(self):
return lookup_func

def fake_entry_points(group):
assert group == "typeguard.checker_lookup"
return [FakeEntryPoint()]
if sys.version_info >= (3, 10):
def fake_entry_points(group):
assert group == "typeguard.checker_lookup"
return [FakeEntryPoint()]
else:
def fake_entry_points():
return {"typeguard.checker_lookup":[FakeEntryPoint()]}

checker_lookup_functions = []
monkeypatch.setattr("typeguard._checkers.entry_points", fake_entry_points)
Expand Down

0 comments on commit 94c1b45

Please sign in to comment.