Skip to content

Commit

Permalink
mino fix of types
Browse files Browse the repository at this point in the history
  • Loading branch information
gkirgizov committed Nov 8, 2022
1 parent fec7d2c commit bf5649b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions fedot/core/serializers/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

# NB: at the end of module init happens registration of default class coders

S = TypeVar('S')
EncodeCallable = Callable[[S], Dict[str, Any]]
DecodeCallable = Callable[[Type[S], Dict[str, Any]], S]
INSTANCE_OR_CALLABLE = TypeVar('INSTANCE_OR_CALLABLE', object, Callable)
EncodeCallable = Callable[[INSTANCE_OR_CALLABLE], Dict[str, Any]]
DecodeCallable = Callable[[Type[INSTANCE_OR_CALLABLE], Dict[str, Any]], INSTANCE_OR_CALLABLE]


MODULE_X_NAME_DELIMITER = '/'
Expand Down Expand Up @@ -99,9 +98,10 @@ def _register_default_coders():
Serializer.CODERS_BY_TYPE.update(default_coders)

@staticmethod
def register_coders(cls: Type[S],
to_json: Optional[EncodeCallable[S]] = None,
from_json: Optional[DecodeCallable[S]] = None) -> Type[S]:
def register_coders(cls: Type[INSTANCE_OR_CALLABLE],
to_json: Optional[EncodeCallable[INSTANCE_OR_CALLABLE]] = None,
from_json: Optional[DecodeCallable[INSTANCE_OR_CALLABLE]] = None,
) -> Type[INSTANCE_OR_CALLABLE]:
"""Registers classes as json-serializable so that they can be used by `Serializer`.
Supports 3 alternative usages:
Expand Down Expand Up @@ -241,15 +241,16 @@ def object_hook(json_obj: Dict[str, Any]) -> Union[INSTANCE_OR_CALLABLE, dict]:
return json_obj


def default_save(obj: Any, json_file_path: Union[str, os.PathLike] = None) -> Optional[str]:
def default_save(obj: Any, json_file_path: Optional[Union[str, os.PathLike]] = None) -> Optional[str]:
""" Default save to json using Serializer """
if json_file_path is None:
return json.dumps(obj, indent=4, cls=Serializer)
with open(json_file_path, mode='w') as json_file:
json.dump(obj, json_file, indent=4, cls=Serializer)
return None


def default_load(json_str_or_file_path: Union[str, os.PathLike] = None) -> Any:
def default_load(json_str_or_file_path: Optional[Union[str, os.PathLike]] = None) -> Any:
""" Default load from json using Serializer """
def load_as_file_path():
with open(json_str_or_file_path, mode='r') as json_file:
Expand All @@ -267,7 +268,7 @@ def load_as_json_str():
return load_as_file_path()


def register_serializable(cls: Type[INSTANCE_OR_CALLABLE] = None,
def register_serializable(cls: Optional[Type[INSTANCE_OR_CALLABLE]] = None,
to_json: Optional[EncodeCallable] = None,
from_json: Optional[DecodeCallable] = None,
add_save_load: bool = False,
Expand Down

0 comments on commit bf5649b

Please # to comment.