Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ exclude_also = ["def __repr__", 'if __name__ == "__main__"']

[tool.mypy]
mypy_path = "EpiLog"
strict = true
warn_unused_ignores = true
allow_redefinition = false
force_uppercase_builtins = true
Expand Down
23 changes: 18 additions & 5 deletions src/EpiLog/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import logging
from dataclasses import dataclass
from time import perf_counter_ns
from typing import Dict, Iterator, Tuple, Union
from types import TracebackType
from typing import Dict, Iterator, Optional, Tuple, Union


# Python < 3.11 support
Expand Down Expand Up @@ -58,17 +59,24 @@ class Units:
Args:
units (Unit): unit definitions

Raises:
ValueError: if modifier value of units is not defined.

"""

units: Tuple[Unit, ...]

def __init__(self, *units: Unit):
def __init__(self, *units: Unit) -> None:
self.units = units
self._update()

def _update(self):
def _update(self) -> None:
for n, current in enumerate(self.units[1:]):
previous = self.units[n]
if previous.modifier is None:
raise ValueError(
f"Modifier value of unit '{previous.unit}' cannot be None."
)
current.base = previous.base * previous.modifier

def __iter__(self) -> Iterator[Unit]:
Expand Down Expand Up @@ -158,10 +166,15 @@ def __enter__(self) -> Self:

return self

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
end: int = perf_counter_ns()

if exc_type is not None:
if exc_type is not None and exc_val is not None:
self.log.error("Traceback:", exc_info=(exc_type, exc_val, exc_tb))
return

Expand Down
8 changes: 7 additions & 1 deletion tests/test_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

from EpiLog import EpiLog
from EpiLog.benchmark import NS_UNITS, BenchMark, Units
from EpiLog.benchmark import NS_UNITS, BenchMark, Unit, Units

from .conftest import _assert_msg_in_output

Expand Down Expand Up @@ -83,6 +83,12 @@ def test_empty_convert_units() -> None:
assert u == "", "Expected empty unit string."


def test_units_raises() -> None:
"""Confirm instantiating units without modifier value raises ValueError."""
with pytest.raises(ValueError):
obj = Units(Unit("a"), Unit("b"))


@pytest.mark.parametrize(
["value", "expected"],
[
Expand Down
Loading