diff --git a/src/iokit/__init__.py b/src/iokit/__init__.py index 30090e6..360245d 100644 --- a/src/iokit/__init__.py +++ b/src/iokit/__init__.py @@ -15,7 +15,7 @@ "save_file", "save_temp", ] -__version__ = "0.1.5" +__version__ = "0.1.6" from .extensions import Dat, Env, Gzip, Json, Jsonl, Tar, Txt, Yaml from .state import State, filter_states, find_state diff --git a/src/iokit/state.py b/src/iokit/state.py index 1998784..8a20786 100644 --- a/src/iokit/state.py +++ b/src/iokit/state.py @@ -134,20 +134,26 @@ def _by_suffix(cls, suffix: str) -> Type[Self]: for kls in cls.__subclasses__(): with suppress(ValueError): return kls._by_suffix(suffix) - return cls + raise ValueError(f"Unknown state suffix '{suffix}'") def cast(self) -> "State": - klass = self._by_suffix(self.name.suffix) - state = klass.__new__(klass) - setattr(state, "_data", self.data) - setattr(state, "_name", self.name) - setattr(state, "_time", self.time) - return state + with suppress(ValueError): + klass = self._by_suffix(self.name.suffix) + state = klass.__new__(klass) + setattr(state, "_data", self.data) + setattr(state, "_name", self.name) + setattr(state, "_time", self.time) + return state + return self def load(self) -> Any: if not self.name.suffix: return self.data.getvalue() - return self.cast().load() + state = self.cast() + if type(state) is State: # pylint: disable=unidiomatic-typecheck + msg = f"Cannot load state with suffix '{self.name.suffix}'" + raise NotImplementedError(msg) + return state.load() def filter_states(states: Iterable[State], pattern: str) -> Generator[State, None, None]: