From b889340a585171f2386fc7cfd51528fa88ac508a Mon Sep 17 00:00:00 2001 From: SaridakisStamatisChristos <34583142+SaridakisStamatisChristos@users.noreply.github.com> Date: Fri, 3 Oct 2025 20:26:39 +0300 Subject: [PATCH] Fix extend compaction bug and ensure tests import package --- kll_sketch/kll_sketch.py | 6 ++++-- kll_sketch/tests/conftest.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 kll_sketch/tests/conftest.py diff --git a/kll_sketch/kll_sketch.py b/kll_sketch/kll_sketch.py index 3afb061..ea220e0 100644 --- a/kll_sketch/kll_sketch.py +++ b/kll_sketch/kll_sketch.py @@ -70,12 +70,14 @@ def add(self, x: float) -> None: self._compress_until_ok() def extend(self, xs: Iterable[float]) -> None: - buf = self._levels[0] for x in xs: xv = float(x) if math.isnan(xv) or math.isinf(xv): raise ValueError("values must be finite") - buf.append(xv) + # ``self._levels[0]`` can be replaced during compaction, so we must + # append directly to the current buffer each iteration instead of + # keeping a stale reference (which would silently drop values). + self._levels[0].append(xv) self._n += 1 if self._capacity_exceeded(): self._compress_until_ok() diff --git a/kll_sketch/tests/conftest.py b/kll_sketch/tests/conftest.py new file mode 100644 index 0000000..984d641 --- /dev/null +++ b/kll_sketch/tests/conftest.py @@ -0,0 +1,13 @@ +"""Pytest configuration ensuring the package is importable during tests.""" +from __future__ import annotations + +import sys +from pathlib import Path + +# When pytest collects tests inside the installed package directory, the +# repository root (which contains the ``kll_sketch`` package) might not be on +# ``sys.path``. Add it explicitly so ``from kll_sketch import KLL`` works even +# when the tests are executed without installing the project as a package. +ROOT = Path(__file__).resolve().parents[2] +if str(ROOT) not in sys.path: + sys.path.insert(0, str(ROOT))