1- # coding: utf8
2- from __future__ import unicode_literals
3-
4- from collections import OrderedDict
5- import sys
1+ from typing import Sequence , Any , Dict , Tuple , Callable , Optional , TypeVar
62
73try : # Python 3.8
84 import importlib .metadata as importlib_metadata
95except ImportError :
10- import importlib_metadata
11-
12- if sys .version_info [0 ] == 2 :
13- basestring_ = basestring # noqa: F821
14- else :
15- basestring_ = str
6+ import importlib_metadata # type: ignore
167
178# Only ever call this once for performance reasons
18- AVAILABLE_ENTRY_POINTS = importlib_metadata .entry_points ()
9+ AVAILABLE_ENTRY_POINTS = importlib_metadata .entry_points () # type: ignore
1910
2011# This is where functions will be registered
21- REGISTRY = OrderedDict ()
12+ REGISTRY : Dict [Tuple [str , ...], Any ] = {}
13+
2214
15+ InFunc = TypeVar ("InFunc" )
2316
24- def create (* namespace , ** kwargs ):
17+
18+ def create (* namespace : str , entry_points : bool = False ) -> "Registry" :
2519 """Create a new registry.
2620
2721 *namespace (str): The namespace, e.g. "spacy" or "spacy", "architectures".
28- RETURNS (Tuple[Callable] ): The setter (decorator to register functions)
29- and getter (to retrieve functions) .
22+ entry_points (bool ): Accept registered functions from entry points.
23+ RETURNS (Registry): The Registry object .
3024 """
31- entry_points = kwargs .get ("entry_points" , False )
3225 if check_exists (* namespace ):
3326 raise RegistryError ("Namespace already exists: {}" .format (namespace ))
3427 return Registry (namespace , entry_points = entry_points )
3528
3629
3730class Registry (object ):
38- def __init__ (self , namespace , entry_points = False ):
31+ def __init__ (self , namespace : Sequence [ str ] , entry_points : bool = False ) -> None :
3932 """Initialize a new registry.
4033
41- namespace (Tuple [str]): The namespace.
34+ namespace (Sequence [str]): The namespace.
4235 entry_points (bool): Whether to also check for entry points.
43- RETURNS (Registry): The newly created object.
4436 """
4537 self .namespace = namespace
4638 self .entry_point_namespace = "_" .join (namespace )
4739 self .entry_points = entry_points
4840
49- def __contains__ (self , name ) :
41+ def __contains__ (self , name : str ) -> bool :
5042 """Check whether a name is in the registry.
5143
5244 name (str): The name to check.
@@ -56,16 +48,20 @@ def __contains__(self, name):
5648 has_entry_point = self .entry_points and self .get_entry_point (name )
5749 return has_entry_point or namespace in REGISTRY
5850
59- def __call__ (self , name , ** kwargs ):
51+ def __call__ (
52+ self , name : str , func : Optional [Any ] = None
53+ ) -> Callable [[InFunc ], InFunc ]:
6054 """Register a function for a given namespace. Same as Registry.register.
6155
6256 name (str): The name to register under the namespace.
6357 func (Any): Optional function to register (if not used as decorator).
6458 RETURNS (Callable): The decorator.
6559 """
66- return self .register (name , ** kwargs )
60+ return self .register (name , func = func )
6761
68- def register (self , name , ** kwargs ):
62+ def register (
63+ self , name : str , * , func : Optional [Any ] = None
64+ ) -> Callable [[InFunc ], InFunc ]:
6965 """Register a function for a given namespace.
7066
7167 name (str): The name to register under the namespace.
@@ -77,12 +73,11 @@ def do_registration(func):
7773 _set (list (self .namespace ) + [name ], func )
7874 return func
7975
80- func = kwargs .get ("func" )
8176 if func is not None :
8277 return do_registration (func )
8378 return do_registration
8479
85- def get (self , name ) :
80+ def get (self , name : str ) -> Any :
8681 """Get the registered function for a given name.
8782
8883 name (str): The name.
@@ -100,14 +95,14 @@ def get(self, name):
10095 raise RegistryError (err .format (name , current_namespace , available ))
10196 return _get (namespace )
10297
103- def get_all (self ):
98+ def get_all (self ) -> Dict [ str , Any ] :
10499 """Get a all functions for a given namespace.
105100
106101 namespace (Tuple[str]): The namespace to get.
107102 RETURNS (Dict[str, Any]): The functions, keyed by name.
108103 """
109104 global REGISTRY
110- result = OrderedDict ()
105+ result = {}
111106 if self .entry_points :
112107 result .update (self .get_entry_points ())
113108 for keys , value in REGISTRY .items ():
@@ -117,7 +112,7 @@ def get_all(self):
117112 result [keys [- 1 ]] = value
118113 return result
119114
120- def get_entry_points (self ):
115+ def get_entry_points (self ) -> Dict [ str , Any ] :
121116 """Get registered entry points from other packages for this namespace.
122117
123118 RETURNS (Dict[str, Any]): Entry points, keyed by name.
@@ -127,7 +122,7 @@ def get_entry_points(self):
127122 result [entry_point .name ] = entry_point .load ()
128123 return result
129124
130- def get_entry_point (self , name , default = None ):
125+ def get_entry_point (self , name : str , default : Optional [ Any ] = None ) -> Any :
131126 """Check if registered entry point is available for a given name in the
132127 namespace and load it. Otherwise, return the default value.
133128
@@ -141,7 +136,7 @@ def get_entry_point(self, name, default=None):
141136 return default
142137
143138
144- def check_exists (* namespace ) :
139+ def check_exists (* namespace : str ) -> bool :
145140 """Check if a namespace exists.
146141
147142 *namespace (str): The namespace.
@@ -150,14 +145,14 @@ def check_exists(*namespace):
150145 return namespace in REGISTRY
151146
152147
153- def _get (namespace ) :
148+ def _get (namespace : Sequence [ str ]) -> Any :
154149 """Get the value for a given namespace.
155150
156- namespace (Tuple [str]): The namespace.
157- RETURNS: The value for the namespace.
151+ namespace (Sequence [str]): The namespace.
152+ RETURNS (Any) : The value for the namespace.
158153 """
159154 global REGISTRY
160- if not all (isinstance (name , basestring_ ) for name in namespace ):
155+ if not all (isinstance (name , str ) for name in namespace ):
161156 err = "Invalid namespace. Expected tuple of strings, but got: {}"
162157 raise ValueError (err .format (namespace ))
163158 namespace = tuple (namespace )
@@ -167,16 +162,16 @@ def _get(namespace):
167162 return REGISTRY [namespace ]
168163
169164
170- def _get_all (namespace ) :
165+ def _get_all (namespace : Sequence [ str ]) -> Dict [ Tuple [ str , ...], Any ] :
171166 """Get all matches for a given namespace, e.g. ("a", "b", "c") and
172167 ("a", "b") for namespace ("a", "b").
173168
174- namespace (Tuple [str]): The namespace.
169+ namespace (Sequence [str]): The namespace.
175170 RETURNS (Dict[Tuple[str], Any]): All entries for the namespace, keyed
176171 by their full namespaces.
177172 """
178173 global REGISTRY
179- result = OrderedDict ()
174+ result = {}
180175 for keys , value in REGISTRY .items ():
181176 if len (namespace ) <= len (keys ) and all (
182177 namespace [i ] == keys [i ] for i in range (len (namespace ))
@@ -185,21 +180,21 @@ def _get_all(namespace):
185180 return result
186181
187182
188- def _set (namespace , func ) :
183+ def _set (namespace : Sequence [ str ] , func : Any ) -> None :
189184 """Set a value for a given namespace.
190185
191- namespace (Tuple [str]): The namespace.
186+ namespace (Sequence [str]): The namespace.
192187 func (Callable): The value to set.
193188 """
194189 global REGISTRY
195190 REGISTRY [tuple (namespace )] = func
196191
197192
198- def _remove (namespace ) :
193+ def _remove (namespace : Sequence [ str ]) -> Any :
199194 """Remove a value for a given namespace.
200195
201- namespace (Tuple [str]): The namespace.
202- RETURNS: The removed value.
196+ namespace (Sequence [str]): The namespace.
197+ RETURNS (Any) : The removed value.
203198 """
204199 global REGISTRY
205200 namespace = tuple (namespace )
0 commit comments