def __init__(self, source_value: Any, object_path: str = "/") -> None:
super().__init__(source_value, object_path=object_path)
if isinstance(source_value, str):
+ self._raw_value: str = source_value
self._value: Path = Path(source_value)
else:
raise ValueError(f"expected file path in a string, got '{source_value}' with type '{type(source_value)}'.")
return self._value
def serialize(self) -> Any:
- return str(self._value)
+ return self._raw_value
@classmethod
def json_schema(cls: Type["UncheckedPath"]) -> Dict[Any, Any]:
import enum
import inspect
+from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union, cast
import yaml
return type(obj) == types
-class Serializable:
+class Serializable(ABC):
"""
An interface for making classes serializable to a dictionary (and in turn into a JSON).
"""
+ @abstractmethod
def to_dict(self) -> Dict[Any, Any]:
raise NotImplementedError(f"...for class {self.__class__.__name__}")
+from abc import ABC, abstractmethod
from typing import Any, Dict, Type
-class BaseValueType:
+class BaseValueType(ABC):
"""
Subclasses of this class can be used as type annotations in 'DataParser'. When a value
is being parsed from a serialized format (e.g. JSON/YAML), an object will be created by
raise a `ValueError` in case of errors.
"""
+ @abstractmethod
def __init__(self, source_value: Any, object_path: str = "/") -> None:
pass
+ @abstractmethod
def __int__(self) -> int:
raise NotImplementedError(f" return 'int()' value for {type(self).__name__} is not implemented.")
+ @abstractmethod
def __str__(self) -> str:
raise NotImplementedError(f"return 'str()' value for {type(self).__name__} is not implemented.")
+ @abstractmethod
def serialize(self) -> Any:
"""
- Every custom type should implement this. It is used for dumping configuration.
+ Used for dumping configuration. Returns a JSON-serializable object from which the object
+ can be recreated again using the constructor.
It's not necessary to return the same structure that was given as an input. It only has
to be the same semantically.
raise NotImplementedError(f"{type(self).__name__}'s' 'serialize()' not implemented.")
@classmethod
+ @abstractmethod
def json_schema(cls: Type["BaseValueType"]) -> Dict[Any, Any]:
raise NotImplementedError()