Skip to content

Commit c080ff4

Browse files
daiyippyglove authors
authored andcommitted
JsonConvertible.add_module_alias to support multiple aliases
PiperOrigin-RevId: 786880724
1 parent 5f38cad commit c080ff4

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

pyglove/core/utils/json_conversion.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import pickle
2424
import types
2525
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
2727

2828
# Nestable[T] is a (maybe) nested structure of T, which could be T, a Dict
2929
# a List or a Tuple of Nestable[T]. We use a Union to fool PyType checker to
@@ -83,9 +83,16 @@ def register(
8383
f'{self._type_to_cls_map[type_name].__name__}.')
8484
self._type_to_cls_map[type_name] = cls
8585

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:
8791
"""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
8996

9097
def is_registered(self, type_name: str) -> bool:
9198
"""Returns whether a type name is registered."""
@@ -245,9 +252,13 @@ def register(
245252
cls._TYPE_REGISTRY.register(type_name, subclass, override_existing)
246253

247254
@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:
249260
"""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)
251262

252263
@classmethod
253264
def is_registered(cls, type_name: str) -> bool:

0 commit comments

Comments
 (0)