From 6f366719472b5c914c15def736db42851735029c Mon Sep 17 00:00:00 2001 From: xillig <56161060+xillig@users.noreply.github.com> Date: Wed, 11 Nov 2026 05:42:34 +0100 Subject: [PATCH] fix(logging): clamp verbosity levels outside 0-3 set_verbosity() mapped any level outside 0-3 to INFO. A level above 3 therefore logged less than 3, and a negative level more than 0: Preprocessor(verbose=4) printed only the fit summary and dropped the per-feature table and the fitted decisions that verbose=2 and 3 log. The level is now clamped into 0-3, so verbosity never decreases as the level grows. This also applies to configure_logging() and to the Preprocessor's verbose parameter, which both go through set_verbosity(). --- docs/core_concepts/outputs_and_inspection.md | 3 ++ pretab/core/logging.py | 6 ++- pretab/preprocessor.py | 2 + tests/integration/test_verbosity.py | 41 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/docs/core_concepts/outputs_and_inspection.md b/docs/core_concepts/outputs_and_inspection.md index 68f1989..5915a98 100644 --- a/docs/core_concepts/outputs_and_inspection.md +++ b/docs/core_concepts/outputs_and_inspection.md @@ -384,6 +384,9 @@ Preprocessor(numerical_method="ple", verbose=1).fit(df, y) fit complete: 2 numerical (ple) + 1 categorical (int) feature(s) -> 15 output columns in 0.02s ``` +Levels are cumulative, and a value above `3` logs like `3` (`set_verbosity` and +`configure_logging` clamp a level into `0`-`3`). + Each fit applies its own level, so a later `verbose=2` fit in the same process or notebook logs the table even after a `verbose=1` fit. PreTab attaches its own stream handler only when no host handler receives its records. A handler on the `"pretab"` logger, or on an ancestor such as the diff --git a/pretab/core/logging.py b/pretab/core/logging.py index cb75f4c..0d30ac2 100644 --- a/pretab/core/logging.py +++ b/pretab/core/logging.py @@ -67,9 +67,11 @@ def set_verbosity(level: int = 1) -> None: level : int, default=1 Verbosity level ``0``-``3`` (``0`` = WARNING, ``1`` = INFO, ``2``/``3`` = DEBUG). ``bool`` is accepted and coerced (``True`` -> ``1``, ``False`` -> - ``0``). + ``0``). A value outside that range is clamped into it, so verbosity never + decreases as the level grows: a negative level is as quiet as ``0`` and a + level above ``3`` is as verbose as ``3``. """ - _LOGGER.setLevel(_LEVELS.get(int(level), logging.INFO)) + _LOGGER.setLevel(_LEVELS[min(max(int(level), min(_LEVELS)), max(_LEVELS))]) def configure_logging(level: int = 1, handler: "logging.Handler | None" = None) -> None: diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index aaba9df..e9cfb1d 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -324,6 +324,8 @@ class Preprocessor(TransformerMixin, BaseEstimator): - ``2`` -- the per-feature table that :meth:`get_feature_info` builds. - ``3`` -- internal decisions (fitted bins / knots / centers). + A value above ``3`` logs like ``3``. + Stored verbatim and forwarded by ``get_params`` / ``clone``, so an embedding host (e.g. DeepTab) can pass it straight through ``Preprocessor(**kwargs)``. PreTab never configures the root logger or attaches a handler when the host already owns one (on diff --git a/tests/integration/test_verbosity.py b/tests/integration/test_verbosity.py index 3526879..29afee8 100644 --- a/tests/integration/test_verbosity.py +++ b/tests/integration/test_verbosity.py @@ -229,6 +229,47 @@ def test_set_verbosity_sets_logger_level(): assert logger.level == logging.INFO +@pytest.mark.parametrize( + ("level", "expected"), + [ + (-1, logging.WARNING), + (0, logging.WARNING), + (3, logging.DEBUG), + (4, logging.DEBUG), + (10, logging.DEBUG), + (True, logging.INFO), + (False, logging.WARNING), + ], +) +def test_set_verbosity_clamps_out_of_range_levels(level, expected): + """A level outside 0-3 fell back to INFO: 4 logged less than 3, and -1 more than 0.""" + set_verbosity(level) + assert logging.getLogger("pretab").level == expected + + +def test_configure_logging_clamps_out_of_range_levels(): + with _root_handlers(): + configure_logging(7) + assert logging.getLogger("pretab").level == logging.DEBUG + configure_logging(-3) + assert logging.getLogger("pretab").level == logging.WARNING + + +def test_verbose_above_three_logs_like_three(caplog, sample_data): + """Preprocessor(verbose=4) dropped the level-2 table and level-3 details.""" + X, y = sample_data + + def records(verbose): + caplog.clear() + with caplog.at_level(logging.DEBUG, logger="pretab"): + Preprocessor(verbose=verbose).fit(X, y) + return [(record.levelno, record.getMessage().split(" in ")[0]) for record in caplog.records] + + level_three = records(3) + assert any(level == logging.DEBUG for level, _ in level_three) + assert records(4) == level_three + + @contextlib.contextmanager def _root_handlers(*handlers): """Run with exactly ``handlers`` on the root logger (none: a plain Python process).