3636 UpdateProcessor ,
3737)
3838from rdflib .exceptions import Error
39- from typing import Type , TypeVar
39+ from typing import (
40+ TYPE_CHECKING ,
41+ Any ,
42+ Dict ,
43+ Generic ,
44+ Iterator ,
45+ Optional ,
46+ Tuple ,
47+ Type ,
48+ TypeVar ,
49+ overload ,
50+ )
51+
52+ if TYPE_CHECKING :
53+ from pkg_resources import EntryPoint
4054
4155__all__ = ["register" , "get" , "plugins" , "PluginException" , "Plugin" , "PKGPlugin" ]
4256
5165 "rdf.plugins.updateprocessor" : UpdateProcessor ,
5266}
5367
54- _plugins = {}
68+ _plugins : Dict [ Tuple [ str , Type [ Any ]], "Plugin" ] = {}
5569
5670
5771class PluginException (Error ):
5872 pass
5973
6074
61- class Plugin (object ):
62- def __init__ (self , name , kind , module_path , class_name ):
75+ PluginT = TypeVar ("PluginT" )
76+
77+
78+ class Plugin (Generic [PluginT ]):
79+ def __init__ (
80+ self , name : str , kind : Type [PluginT ], module_path : str , class_name : str
81+ ):
6382 self .name = name
6483 self .kind = kind
6584 self .module_path = module_path
6685 self .class_name = class_name
67- self ._class = None
86+ self ._class : Optional [ Type [ PluginT ]] = None
6887
69- def getClass (self ):
88+ def getClass (self ) -> Type [ PluginT ] :
7089 if self ._class is None :
7190 module = __import__ (self .module_path , globals (), locals (), ["" ])
7291 self ._class = getattr (module , self .class_name )
7392 return self ._class
7493
7594
76- class PKGPlugin (Plugin ):
77- def __init__ (self , name , kind , ep ):
95+ class PKGPlugin (Plugin [ PluginT ] ):
96+ def __init__ (self , name : str , kind : Type [ PluginT ] , ep : "EntryPoint" ):
7897 self .name = name
7998 self .kind = kind
8099 self .ep = ep
81- self ._class = None
100+ self ._class : Optional [ Type [ PluginT ]] = None
82101
83- def getClass (self ):
102+ def getClass (self ) -> Type [ PluginT ] :
84103 if self ._class is None :
85104 self ._class = self .ep .load ()
86105 return self ._class
87106
88107
89- def register (name : str , kind , module_path , class_name ):
108+ def register (name : str , kind : Type [ Any ] , module_path , class_name ):
90109 """
91110 Register the plugin for (name, kind). The module_path and
92111 class_name should be the path to a plugin class.
@@ -95,16 +114,13 @@ def register(name: str, kind, module_path, class_name):
95114 _plugins [(name , kind )] = p
96115
97116
98- PluginT = TypeVar ("PluginT" )
99-
100-
101117def get (name : str , kind : Type [PluginT ]) -> Type [PluginT ]:
102118 """
103119 Return the class for the specified (name, kind). Raises a
104120 PluginException if unable to do so.
105121 """
106122 try :
107- p = _plugins [(name , kind )]
123+ p : Plugin [ PluginT ] = _plugins [(name , kind )]
108124 except KeyError :
109125 raise PluginException ("No plugin registered for (%s, %s)" % (name , kind ))
110126 return p .getClass ()
@@ -121,7 +137,21 @@ def get(name: str, kind: Type[PluginT]) -> Type[PluginT]:
121137 _plugins [(ep .name , kind )] = PKGPlugin (ep .name , kind , ep )
122138
123139
124- def plugins (name = None , kind = None ):
140+ @overload
141+ def plugins (
142+ name : Optional [str ] = ..., kind : Type [PluginT ] = ...
143+ ) -> Iterator [Plugin [PluginT ]]:
144+ ...
145+
146+
147+ @overload
148+ def plugins (name : Optional [str ] = ..., kind : None = ...) -> Iterator [Plugin ]:
149+ ...
150+
151+
152+ def plugins (
153+ name : Optional [str ] = None , kind : Optional [Type [PluginT ]] = None
154+ ) -> Iterator [Plugin ]:
125155 """
126156 A generator of the plugins.
127157
0 commit comments