Skip to content

Commit

Permalink
chore: more mypy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
makkus committed Feb 6, 2024
1 parent 777f7cd commit edbc6bb
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 177 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ module = [
"importlib_metadata.*",
"importlib_resources.*",
"jupytext",
"lz4.*",
"mmh3",
"pickle5",
"pp",
Expand All @@ -517,6 +518,7 @@ module = [
"streamlit.*",
"textual.*",
"uvloop",
"uvicorn"
"uvicorn",
"zstandard"
]
ignore_missing_imports = true
2 changes: 1 addition & 1 deletion src/kiara/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def module_type_names(self) -> Iterable[str]:

def register_external_archive(
self,
archive: Union[str, KiaraArchive, List[KiaraArchive], List[str]],
archive: Union[str, KiaraArchive, Iterable[Union[KiaraArchive, str]]],
allow_write_access: bool = False,
) -> Dict[str, str]:
"""Register one or several external archives with the context.
Expand Down
4 changes: 3 additions & 1 deletion src/kiara/context/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def load_existing_archive(

if store_type:
if isinstance(store_type, str):
archive_cls: Union[Type[KiaraArchive], None] = archive_types.get(store_type, None)
archive_cls: Union[Type[KiaraArchive], None] = archive_types.get(
store_type, None
)
if archive_cls is None:
raise Exception(
f"Can't create context: no archive type '{store_type}' available. Available types: {', '.join(archive_types.keys())}"
Expand Down
11 changes: 8 additions & 3 deletions src/kiara/interfaces/cli/data/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,14 @@ def import_data_store(ctx, archive: str):
terminal_print("Registering data archive...")
store_alias = kiara_api.context.data_registry.register_data_archive(data_archive)

result = kiara_api.store_values(
values=data_archive.value_ids, alias_store=store_alias
)
values = data_archive.value_ids
if values is None:
terminal_print(
f"[red]Error[/red]: No values found in '{archive}', probably because the archive type is incompatible."
)
sys.exit(1)

result = kiara_api.store_values(values=values, alias_store=store_alias)
terminal_print(result)

terminal_print("Done.")
3 changes: 2 additions & 1 deletion src/kiara/interfaces/python_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,12 +1745,13 @@ def store_values(
else:
for field_name, value in values.items():
if alias_map is False:
aliases = None
aliases: Union[None, Iterable[str]] = None
elif alias_map is True:
aliases = [field_name]
elif isinstance(alias_map, str):
aliases = [f"{alias_map}.{field_name}"]
else:
# means it's a mapping
aliases = alias_map.get(field_name)

value_obj = self.get_value(value)
Expand Down
6 changes: 2 additions & 4 deletions src/kiara/interfaces/python_api/models/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2074,9 +2074,7 @@ def create_renderable(self, **config: Any) -> RenderableType:
table.add_row("Job ID", str(self.job_record.job_id))
runtime_details = self.job_record.runtime_details
assert runtime_details is not None
table.add_row(
"Job details", runtime_details.create_renderable(**config)
)
table.add_row("Job details", runtime_details.create_renderable(**config))
table.add_row("Operation details", self.operation)

values_table = Table(show_header=False, box=box.SIMPLE)
Expand Down Expand Up @@ -2117,7 +2115,7 @@ def create_renderable(self, **config: Any) -> RenderableType:

info: JobInfo
for info in self.item_infos.values(): # type: ignore

runtime_details = info.job_record.runtime_details
assert runtime_details is not None
row: List[RenderableType] = []
Expand Down
14 changes: 8 additions & 6 deletions src/kiara/registries/aliases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def aliases_by_id(self) -> Mapping[uuid.UUID, Set[AliasItem]]:
def dynamic_aliases(self) -> Dict[str, AliasItem]:
if self._cached_dynamic_aliases is None:
self.aliases
return self._cached_dynamic_aliases
return self._cached_dynamic_aliases # type: ignore

@property
def aliases(self) -> Mapping[str, AliasItem]:
Expand Down Expand Up @@ -269,7 +269,7 @@ def aliases(self) -> Mapping[str, AliasItem]:
def dynamic_stores(self) -> List[str]:
if self._dynamic_stores is None:
self.aliases
return self._dynamic_stores
return self._dynamic_stores # type: ignore

def find_value_id_for_alias(self, alias: str) -> Union[uuid.UUID, None]:
"""Find the value id for a given alias.
Expand All @@ -289,7 +289,7 @@ def find_value_id_for_alias(self, alias: str) -> Union[uuid.UUID, None]:
return alias_item.value_id

if "#" not in alias:
archive_alias = self.default_alias_store
archive_alias: Union[str, None] = self.default_alias_store
rest = alias
else:
mountpoint, rest = alias.split("#", maxsplit=1)
Expand All @@ -302,6 +302,8 @@ def find_value_id_for_alias(self, alias: str) -> Union[uuid.UUID, None]:
return None

archive = self.get_archive(archive_alias=archive_alias)
if archive is None:
raise Exception(f"Invalid alias store: '{archive_alias}' not registered.")
result_value_id = archive.find_value_id_for_alias(alias=rest)
if result_value_id:
alias_item = AliasItem(
Expand Down Expand Up @@ -376,7 +378,7 @@ def register_aliases(
alias_store: Union[str, None] = None,
):

aliases_to_store = {}
aliases_to_store: Dict[str, List[str]] = {}
for alias in aliases:
if "#" in alias:
mountpoint, alias_name = alias.split("#", maxsplit=1)
Expand Down Expand Up @@ -466,8 +468,8 @@ def register_aliases(
logger.info("alias.replace", alias=actual_alias)
# raise NotImplementedError()

self.aliases[actual_alias] = alias_item
self._cached_aliases_by_id.setdefault(value_id, set()).add(alias_item)
self.aliases[actual_alias] = alias_item # type: ignore
self._cached_aliases_by_id.setdefault(value_id, set()).add(alias_item) # type: ignore


#
Expand Down
9 changes: 2 additions & 7 deletions src/kiara/registries/aliases/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path
from typing import Any, Mapping, Set, Union

from orjson import orjson
import orjson

from kiara.registries import ARCHIVE_CONFIG_CLS, FileSystemArchiveConfig
from kiara.registries.aliases import AliasArchive, AliasStore
Expand All @@ -37,13 +37,9 @@ def __init__(
)

self._base_path: Union[Path, None] = None
self._archive_metadata: Union[Mapping[str, Any], None] = None

def _retrieve_archive_metadata(self) -> Mapping[str, Any]:

if self._archive_metadata is not None:
return self._archive_metadata

if not self.archive_metadata_path.is_file():
_archive_metadata = {}
else:
Expand All @@ -59,8 +55,7 @@ def _retrieve_archive_metadata(self) -> Mapping[str, Any]:
f"Could not retrieve archive id for alias archive '{self.archive_alias}'."
)

self._archive_metadata = _archive_metadata
return self._archive_metadata
return _archive_metadata

@property
def alias_store_path(self) -> Path:
Expand Down
11 changes: 6 additions & 5 deletions src/kiara/registries/aliases/sqlite_store.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
import uuid
from pathlib import Path
from typing import Any, Mapping, Set, Union
from typing import Any, Dict, Mapping, Set, Union

from sqlalchemy import Engine, create_engine, text
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Engine

from kiara.registries import SqliteArchiveConfig
from kiara.registries.aliases import AliasArchive, AliasStore
Expand All @@ -18,7 +19,7 @@ class SqliteAliasArchive(AliasArchive):
@classmethod
def _load_archive_config(
cls, archive_uri: str, allow_write_access: bool, **kwargs
) -> Union[Mapping[str, Any], None]:
) -> Union[Dict[str, Any], None]:

if allow_write_access:
return None
Expand Down Expand Up @@ -55,7 +56,7 @@ def __init__(
AliasArchive.__init__(
self,
archive_alias=archive_alias,
archive_config=archive_config,
archive_config=archive_config, # type: ignore
force_read_only=force_read_only,
)
self._db_path: Union[Path, None] = None
Expand Down Expand Up @@ -147,7 +148,7 @@ class SqliteAliasStore(SqliteAliasArchive, AliasStore):
@classmethod
def _load_archive_config(
cls, archive_uri: str, allow_write_access: bool, **kwargs
) -> Union[Mapping[str, Any], None]:
) -> Union[Dict[str, Any], None]:

if not allow_write_access:
return None
Expand Down
59 changes: 31 additions & 28 deletions src/kiara/registries/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def resolve_alias(self, alias: str) -> uuid.UUID:
_value_id = uuid.UUID(default_value)
else:
from kiara.registries.aliases import AliasArchive

alias_archive: AliasArchive = archives.get("alias", None) # type: ignore
if alias_archive:
_value_id = alias_archive.find_value_id_for_alias(
Expand All @@ -171,7 +171,9 @@ def resolve_alias(self, alias: str) -> uuid.UUID:
msg=f"No alias archive found for '{archive_ref}'."
)
else:
raise NoSuchValueException("No data archive found in '{archive_ref}'.")
raise NoSuchValueException(
"No data archive found in '{archive_ref}'."
)
else:
raise NoSuchValueException(
msg=f"No archive found for '{archive_ref}'."
Expand Down Expand Up @@ -490,34 +492,35 @@ def store_value(
again, the archive_id is used, if not, the string is used as the archive alias.
"""

if data_store is None:
data_store = self.default_data_store

_value = self.get_value(value)

store: DataStore = self.get_archive(archive_id_or_alias=data_store) # type: ignore
if not isinstance(store, DataStore):
raise Exception(
f"Can't store value into store '{data_store}': not writable."
)
if not store.is_writeable():
if data_store:
raise Exception(
f"Can't store value into store '{data_store}': not writable."
)
else:
raise Exception("Can't store value into store: not writable.")

_data_store = store.archive_alias
# make sure all property values are available
if _value.pedigree != ORPHAN:
for value_id in _value.pedigree.inputs.values():
self.store_value(value=value_id, data_store=data_store)
self.store_value(value=value_id, data_store=_data_store)

if not store.has_value(_value.value_id):
event = ValuePreStoreEvent(kiara_id=self._kiara.id, value=_value)
self._event_callback(event)
persisted_value = store.store_value(_value)
_value._is_stored = True
self._value_archive_lookup_map[_value.value_id] = data_store

self._value_archive_lookup_map[_value.value_id] = _data_store
self._persisted_value_descs[_value.value_id] = persisted_value
property_values = _value.property_values

for property, property_value in property_values.items():
self.store_value(value=property_value, data_store=data_store)
self.store_value(value=property_value, data_store=_data_store)
else:
persisted_value = None

Expand Down Expand Up @@ -1073,21 +1076,21 @@ def retrieve_serialized_value(

return pv

def retrieve_chunk(
self,
chunk_id: str,
archive_id: Union[uuid.UUID, None] = None,
as_file: bool = True,
symlink_ok: bool = True,
) -> Union[str, "BytesLike"]:

if archive_id is None:
raise NotImplementedError()

archive = self.get_archive(archive_id)
chunk = archive.retrieve_chunk(chunk_id, as_file=as_file, symlink_ok=symlink_ok)

return chunk
# def retrieve_chunk(
# self,
# chunk_id: str,
# archive_id: Union[uuid.UUID, None] = None,
# as_file: bool = True,
# symlink_ok: bool = True,
# ) -> Union[str, "BytesLike"]:
#
# if archive_id is None:
# raise NotImplementedError()
#
# archive = self.get_archive(archive_id)
# chunk = archive.retrieve_chunk(chunk_id, as_file=as_file, symlink_ok=symlink_ok)
#
# return chunk

def retrieve_chunks(
self,
Expand Down
24 changes: 12 additions & 12 deletions src/kiara/registries/data/data_store/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,23 @@ def _find_destinies_for_value(
For now, you can just return 'None' in your implementation.
"""

@abc.abstractmethod
def retrieve_chunk(
self,
chunk_id: str,
as_file: Union[bool, None] = None,
symlink_ok: bool = True,
) -> Union["BytesLike", str]:
"""Retrieve the chunk with the specified id.
If 'as_file' is specified, the chunk is written to a file, and the file path is returned. Otherwise, the chunk is returned as 'bytes'.
"""
# @abc.abstractmethod
# def retrieve_chunk(
# self,
# chunk_id: str,
# as_file: Union[bool, None] = None,
# symlink_ok: bool = True,
# ) -> Union["BytesLike", str]:
# """Retrieve the chunk with the specified id.
#
# If 'as_file' is specified, the chunk is written to a file, and the file path is returned. Otherwise, the chunk is returned as 'bytes'.
# """

@abc.abstractmethod
def retrieve_chunks(
self,
chunk_ids: Sequence[str],
as_files: Union[bool, None] = None,
as_files: bool = True,
symlink_ok: bool = True,
) -> Generator[Union["BytesLike", str], None, None]:
"""Retrieve a generator with all the specified chunks.
Expand Down
Loading

0 comments on commit edbc6bb

Please sign in to comment.