|
23 | 23 | import pickle |
24 | 24 | import types |
25 | 25 | import typing |
26 | | -from typing import Any, Callable, ContextManager, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Type, TypeVar, Union |
| 26 | +from typing import Any, Callable, ContextManager, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, Type, TypeVar, Union |
27 | 27 |
|
28 | 28 | # Nestable[T] is a (maybe) nested structure of T, which could be T, a Dict |
29 | 29 | # a List or a Tuple of Nestable[T]. We use a Union to fool PyType checker to |
@@ -83,9 +83,16 @@ def register( |
83 | 83 | f'{self._type_to_cls_map[type_name].__name__}.') |
84 | 84 | self._type_to_cls_map[type_name] = cls |
85 | 85 |
|
86 | | - def add_module_alias(self, module: str, alias: str) -> None: |
| 86 | + def add_module_alias( |
| 87 | + self, |
| 88 | + module: str, |
| 89 | + alias: Union[str, Sequence[str]] |
| 90 | + ) -> None: |
87 | 91 | """Maps a module name to another name. Usually due to rename.""" |
88 | | - self._prefix_mapping[alias] = module |
| 92 | + if isinstance(alias, str): |
| 93 | + alias = [alias] |
| 94 | + for name in alias: |
| 95 | + self._prefix_mapping[name] = module |
89 | 96 |
|
90 | 97 | def is_registered(self, type_name: str) -> bool: |
91 | 98 | """Returns whether a type name is registered.""" |
@@ -245,9 +252,13 @@ def register( |
245 | 252 | cls._TYPE_REGISTRY.register(type_name, subclass, override_existing) |
246 | 253 |
|
247 | 254 | @classmethod |
248 | | - def add_module_alias(cls, source_name: str, target_name: str) -> None: |
| 255 | + def add_module_alias( |
| 256 | + cls, |
| 257 | + module: str, |
| 258 | + alias: Union[str, Sequence[str]] |
| 259 | + ) -> None: |
249 | 260 | """Adds a module alias so previous serialized objects could be loaded.""" |
250 | | - cls._TYPE_REGISTRY.add_module_alias(source_name, target_name) |
| 261 | + cls._TYPE_REGISTRY.add_module_alias(module, alias) |
251 | 262 |
|
252 | 263 | @classmethod |
253 | 264 | def is_registered(cls, type_name: str) -> bool: |
|
0 commit comments