summaryrefslogtreecommitdiff
path: root/rdflib/plugin.py
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2021-10-17 16:34:55 +0200
committerIwan Aucamp <aucampia@gmail.com>2021-10-24 20:28:49 +0200
commit9a9c9f3326865a382409ab6cea4fd5827ea4e0be (patch)
tree78322709df483aad09dbcad4474e22636c10a3fe /rdflib/plugin.py
parentd9e86c932695d780bcdcdfadda150e4c1bcf72ef (diff)
downloadrdflib-9a9c9f3326865a382409ab6cea4fd5827ea4e0be.tar.gz
Add type hints
This commit only adds type hints and comments and does not make any changes that should affect runtime. The type hints added here derive from work done for #1418.
Diffstat (limited to 'rdflib/plugin.py')
-rw-r--r--rdflib/plugin.py62
1 files changed, 46 insertions, 16 deletions
diff --git a/rdflib/plugin.py b/rdflib/plugin.py
index 7ae8e29c..c5ffd093 100644
--- a/rdflib/plugin.py
+++ b/rdflib/plugin.py
@@ -36,8 +36,22 @@ from rdflib.query import (
UpdateProcessor,
)
from rdflib.exceptions import Error
-from typing import Type, TypeVar
import sys
+from typing import (
+ TYPE_CHECKING,
+ Any,
+ Dict,
+ Generic,
+ Iterator,
+ Optional,
+ Tuple,
+ Type,
+ TypeVar,
+ overload,
+)
+
+if TYPE_CHECKING:
+ from pkg_resources import EntryPoint
__all__ = ["register", "get", "plugins", "PluginException", "Plugin", "PKGPlugin"]
@@ -52,42 +66,47 @@ rdflib_entry_points = {
"rdf.plugins.updateprocessor": UpdateProcessor,
}
-_plugins = {}
+_plugins: Dict[Tuple[str, Type[Any]], "Plugin"] = {}
class PluginException(Error):
pass
-class Plugin(object):
- def __init__(self, name, kind, module_path, class_name):
+PluginT = TypeVar("PluginT")
+
+
+class Plugin(Generic[PluginT]):
+ def __init__(
+ self, name: str, kind: Type[PluginT], module_path: str, class_name: str
+ ):
self.name = name
self.kind = kind
self.module_path = module_path
self.class_name = class_name
- self._class = None
+ self._class: Optional[Type[PluginT]] = None
- def getClass(self):
+ def getClass(self) -> Type[PluginT]:
if self._class is None:
module = __import__(self.module_path, globals(), locals(), [""])
self._class = getattr(module, self.class_name)
return self._class
-class PKGPlugin(Plugin):
- def __init__(self, name, kind, ep):
+class PKGPlugin(Plugin[PluginT]):
+ def __init__(self, name: str, kind: Type[PluginT], ep: "EntryPoint"):
self.name = name
self.kind = kind
self.ep = ep
- self._class = None
+ self._class: Optional[Type[PluginT]] = None
- def getClass(self):
+ def getClass(self) -> Type[PluginT]:
if self._class is None:
self._class = self.ep.load()
return self._class
-def register(name: str, kind, module_path, class_name):
+def register(name: str, kind: Type[Any], module_path, class_name):
"""
Register the plugin for (name, kind). The module_path and
class_name should be the path to a plugin class.
@@ -96,16 +115,13 @@ def register(name: str, kind, module_path, class_name):
_plugins[(name, kind)] = p
-PluginT = TypeVar("PluginT")
-
-
def get(name: str, kind: Type[PluginT]) -> Type[PluginT]:
"""
Return the class for the specified (name, kind). Raises a
PluginException if unable to do so.
"""
try:
- p = _plugins[(name, kind)]
+ p: Plugin[PluginT] = _plugins[(name, kind)]
except KeyError:
raise PluginException("No plugin registered for (%s, %s)" % (name, kind))
return p.getClass()
@@ -128,7 +144,21 @@ else:
_plugins[(ep.name, kind)] = PKGPlugin(ep.name, kind, ep)
-def plugins(name=None, kind=None):
+@overload
+def plugins(
+ name: Optional[str] = ..., kind: Type[PluginT] = ...
+) -> Iterator[Plugin[PluginT]]:
+ ...
+
+
+@overload
+def plugins(name: Optional[str] = ..., kind: None = ...) -> Iterator[Plugin]:
+ ...
+
+
+def plugins(
+ name: Optional[str] = None, kind: Optional[Type[PluginT]] = None
+) -> Iterator[Plugin]:
"""
A generator of the plugins.