Skip to content

Commit

Permalink
chore: fix test, refactor to untangle circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
a.pirogov committed Nov 2, 2023
1 parent e426757 commit d294040
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 165 deletions.
198 changes: 99 additions & 99 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ licensecheck = "licensecheck" # run this when you add new deps
addopts = "--cov-report=term-missing:skip-covered"
filterwarnings = [
"ignore::DeprecationWarning:bokeh.core.*",
"ignore::DeprecationWarning:jupyter_client.*",
]

[tool.coverage.run]
Expand Down
2 changes: 1 addition & 1 deletion src/metador_core/container/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import h5py

from ..ih5.container import IH5Record
from .protocols import H5FileLike, OpenMode
from ..util.types import H5FileLike, OpenMode


class MetadorDriverEnum(Enum):
Expand Down
2 changes: 1 addition & 1 deletion src/metador_core/container/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from ..schema import MetadataSchema
from ..schema.plugins import PluginPkgMeta, PluginRef
from ..schema.types import SemVerTuple
from ..util.types import H5DatasetLike, H5FileLike, H5GroupLike
from . import utils as M
from .drivers import (
METADOR_DRIVERS,
Expand All @@ -38,7 +39,6 @@
get_driver_type,
get_source,
)
from .protocols import H5DatasetLike, H5FileLike, H5GroupLike

if TYPE_CHECKING:
from .wrappers import MetadorContainer, MetadorNode
Expand Down
2 changes: 1 addition & 1 deletion src/metador_core/container/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import h5py
import wrapt

from ..util.types import H5DatasetLike, H5FileLike, H5GroupLike, H5NodeLike, OpenMode
from . import utils as M
from .drivers import MetadorDriver, to_h5filelike
from .interface import MetadorContainerTOC, MetadorMeta, NodeAcl, NodeAclFlags
from .protocols import H5DatasetLike, H5FileLike, H5GroupLike, H5NodeLike, OpenMode


class UnsupportedOperationError(AttributeError):
Expand Down
11 changes: 7 additions & 4 deletions src/metador_core/ih5/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import h5py
import numpy as np

from ..container.protocols import H5DatasetLike
from ..util.types import H5DatasetLike

if TYPE_CHECKING:
from ..container.protocols import H5GroupLike
from ..util.types import H5GroupLike
from .record import IH5Record
else:
IH5Record = Any
Expand Down Expand Up @@ -346,8 +346,11 @@ def _find(self, key: str) -> Optional[int]:
def get(self, key: str, default=None):
try:
return self[key]
except KeyError:
return default
except KeyError as e:
if str(e).find("not open") < 0:
return default
else:
raise

def __getitem__(self, key: str):
self._guard_open()
Expand Down
4 changes: 2 additions & 2 deletions src/metador_core/ih5/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from pydantic import BaseModel, Field, PrivateAttr
from typing_extensions import Annotated, Final

from ..container.protocols import _OPEN_MODES, OpenMode
from ..schema.types import QualHashsumStr
from ..util.hashsums import qualified_hashsum
from ..util.types import OPEN_MODES, OpenMode
from .overlay import IH5Group, h5_copy_from_to

# the magic string we use to identify a valid container
Expand Down Expand Up @@ -472,7 +472,7 @@ def __init__(
paths = None
path: Path = Path(record)

if mode not in _OPEN_MODES:
if mode not in OPEN_MODES:
raise ValueError(f"Unknown file open mode: {mode}")

if mode[0] == "w" or mode == "x":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
runtime_checkable,
)

from typing_extensions import get_args
from .typing import get_args

OpenMode = Literal["r", "r+", "a", "w", "w-", "x"]
"""User open modes that can be passed during initialization."""

OPEN_MODES = list(get_args(OpenMode))


@runtime_checkable
Expand Down Expand Up @@ -138,11 +143,6 @@ def copy(self, source, dest, **kwargs) -> None:
...


OpenMode = Literal["r", "r+", "a", "w", "w-", "x"]
"""User open modes that can be passed during initialization."""
_OPEN_MODES = list(get_args(OpenMode))


@runtime_checkable
class H5FileLike(H5GroupLike, Protocol): # pragma: no cover
"""A HDF5 File acts like the root group and has some extra features."""
Expand All @@ -163,4 +163,4 @@ def __exit__(self, ex_type, ex_value, ex_traceback) -> None:
...


__all__ = ["H5FileLike", "H5GroupLike", "H5DatasetLike", "H5NodeLike", "OpenMode"]
__all__ = ["H5FileLike", "H5GroupLike", "H5DatasetLike", "H5NodeLike"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,7 @@
get_source,
to_h5filelike,
)
from metador_core.container.protocols import (
H5DatasetLike,
H5FileLike,
H5GroupLike,
H5NodeLike,
)


def check_protocols_for(f):
# create group and dataset node
f["group/dataset"] = 123

# check that behavior is correct:

# file-like:
assert isinstance(f, H5NodeLike)
assert isinstance(f, H5FileLike)
assert isinstance(f, H5GroupLike)
assert not isinstance(f, H5DatasetLike)

# group-like:
g = f["group"]
assert isinstance(f, H5NodeLike)
assert not isinstance(g, H5FileLike)
assert isinstance(g, H5GroupLike)
assert not isinstance(g, H5DatasetLike)

# dataset-like:
d = g["dataset"]
assert isinstance(f, H5NodeLike)
assert not isinstance(d, H5FileLike)
assert not isinstance(d, H5GroupLike)
assert isinstance(d, H5DatasetLike)


def test_instance_checks(fresh_mc):
# Check that all driver classes work correctly with the protocol types.
#
# Expected relationships (< = base_class_of):
# H5DatasetLike > H5NodeLike < H5GroupLike < H5FileLike
check_protocols_for(fresh_mc.__wrapped__)


def test_instance_checks_wrapped(fresh_mc):
# same, but wrapped with MetadorContainer
check_protocols_for(fresh_mc)


# ----
from metador_core.util.types import H5DatasetLike, H5GroupLike


def test_driver_type_detection(tmp_mc_path, mc_driver):
Expand Down
41 changes: 41 additions & 0 deletions tests/container/test_container_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from metador_core.util.types import H5DatasetLike, H5FileLike, H5GroupLike, H5NodeLike


def check_protocols_for(f):
# create group and dataset node
f["group/dataset"] = 123

# check that behavior is correct:

# file-like:
assert isinstance(f, H5NodeLike)
assert isinstance(f, H5FileLike)
assert isinstance(f, H5GroupLike)
assert not isinstance(f, H5DatasetLike)

# group-like:
g = f["group"]
assert isinstance(f, H5NodeLike)
assert not isinstance(g, H5FileLike)
assert isinstance(g, H5GroupLike)
assert not isinstance(g, H5DatasetLike)

# dataset-like:
d = g["dataset"]
assert isinstance(f, H5NodeLike)
assert not isinstance(d, H5FileLike)
assert not isinstance(d, H5GroupLike)
assert isinstance(d, H5DatasetLike)


def test_instance_checks(fresh_mc):
# Check that all driver classes work correctly with the protocol types.
#
# Expected relationships (< = base_class_of):
# H5DatasetLike > H5NodeLike < H5GroupLike < H5FileLike
check_protocols_for(fresh_mc.__wrapped__)


def test_instance_checks_wrapped(fresh_mc):
# same, but wrapped with MetadorContainer
check_protocols_for(fresh_mc)
2 changes: 1 addition & 1 deletion tests/ih5/test_ih5_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def test_not_open_fail(dummy_ds_factory):
ds.close()

def assert_ex(f):
with pytest.raises(ValueError) as e:
with pytest.raises(KeyError) as e:
f()
assert str(e).lower().find("not open") >= 0

Expand Down

0 comments on commit d294040

Please sign in to comment.