Repository navigation
Description
Suggested contributor: @Ananyapam7 could work on this issue.
Description
The Preprocessor.policy docstring says the policy governs "constant columns, out-of-range values, missing values, and non-finite inputs". RepresentationPolicy has only constant and out_of_range, and its own module docstring says missing and non-finite values are handled elsewhere (missing_policy, input validation). It also says out_of_range "is not yet threaded through Preprocessor". Following the facade docstring:
policy={"missing": "error"}or{"non_finite": "error"}fails with a rawTypeError: RepresentationPolicy.__init__() got an unexpected keyword argument ...fromRepresentationPolicy.resolve;- standalone transformers reject the same key with a typed
InvalidParamError; - an invalid value (
{"constant": "eror"}) does get the typed error.
Affected code
pretab/preprocessor.py:286-290: thepolicydocstring.pretab/core/policy.py:84-91(RepresentationPolicy.resolve):return cls(**policy)without validating the keys.pretab/core/policy.py:1-34: the module docstring with the actual scope.
Reproduction
import numpy as np
import pandas as pd
from pretab import Preprocessor
X = pd.DataFrame({"a": np.linspace(0, 1, 30)})
for policy in ({"missing": "error"}, {"non_finite": "error"}, {"constant": "eror"}):
try:
Preprocessor(numerical_method="minmax", policy=policy).fit(X)
print(policy, "-> fitted")
except Exception as e:
print(policy, "->", type(e).__name__ + ":", str(e).splitlines()[0])Output (PreTab 1.1.0 (43e97f5), Python 3.12, numpy 2.4.6, pandas 2.3.3, scikit-learn 1.9.0, scipy 1.17.1):
{'missing': 'error'} -> TypeError: RepresentationPolicy.__init__() got an unexpected keyword argument 'missing'
{'non_finite': 'error'} -> TypeError: RepresentationPolicy.__init__() got an unexpected keyword argument 'non_finite'
{'constant': 'eror'} -> InvalidParamError: RepresentationPolicy.constant = 'eror' is invalid.
Expected behavior
The docstring describes the axes that exist and what Preprocessor applies (constant; out_of_range only for standalone transformers). An unknown key raises InvalidParamError listing the valid axes, as the standalone path does.
Actual behavior
Unknown keys raise TypeError, and the documented axes do not exist.
Impact
Users following the facade docstring get an untyped crash, and may believe out-of-range, missing and non-finite handling is configured through policy when it is not.
Root cause
The facade docstring overstates the policy's scope, and resolve passes the mapping straight to the dataclass constructor.
Suggested fix
- In
RepresentationPolicy.resolve, validate the mapping keys against the dataclass fields (the checkmergealready does) and raiseInvalidParamErrorlisting the valid axes. - Correct the
Preprocessor.policydocstring. The facade appliesconstant;out_of_rangeis honoured by the standalone spline transformers but not yet passed to the blocks thePreprocessorbuilds; missing values go throughmissing_policy; non-finite values always raise.
Patch sketch (prototyped on 43e97f5)
diff --git a/pretab/core/policy.py b/pretab/core/policy.py
index 9c49484..902c84d 100644
--- a/pretab/core/policy.py
+++ b/pretab/core/policy.py
@@ -88,6 +88,12 @@ class RepresentationPolicy:
if isinstance(policy, cls):
return policy
if isinstance(policy, dict):
+ valid = {f.name for f in fields(cls)}
+ unknown = sorted(set(policy) - valid, key=str)
+ if unknown:
+ raise invalid_param_error(
+ "RepresentationPolicy", "policy", unknown, f"keys must be among {sorted(valid)}", valid=valid
+ )
return cls(**policy)
raise invalid_param_error(
"RepresentationPolicy",
diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py
index 59879a7..c8d0a53 100644
--- a/pretab/preprocessor.py
+++ b/pretab/preprocessor.py
@@ -284,10 +284,13 @@ class Preprocessor(TransformerMixin, BaseEstimator):
``__missing`` column per feature that stays outside the ordinary basis, so a
downstream model can learn a separate response to missingness.
policy : RepresentationPolicy or dict or None, default=None
- Central edge-case policy (see :class:`~pretab.RepresentationPolicy`) governing how
- constant columns, out-of-range values, missing values, and non-finite inputs are
- handled. ``None`` uses the default policy, which reproduces the library's historical
- behaviour. Pass a mapping such as ``{"constant": "error"}`` to tighten a single axis.
+ Edge-case policy (see :class:`~pretab.RepresentationPolicy`). The ``Preprocessor``
+ applies its ``constant`` axis to the numerical columns at ``fit``; ``out_of_range``
+ is honoured by the standalone spline transformers but not yet passed to the blocks
+ built here. Missing values are configured with ``missing_policy``, and non-finite
+ values always raise. ``None`` uses the default policy, which reproduces the library's
+ historical behaviour. Pass a mapping such as ``{"constant": "error"}`` to tighten a
+ single axis; an unknown key raises ``InvalidParamError``.
max_output_features : int or None, default=None
Upper bound on the total number of output columns produced across all input
features. ``None`` disables the check. A violation is handled perThe patch sketch above was applied to 43e97f5 and checked locally (Python 3.12): the reproduction above then shows the expected behaviour, and the full test suite and pyright were run (4676 passed; pyright 0 errors; strict docs build succeeds). It is a starting point, not a finished PR: tests and docs below still need to be added.
Existing tests affected
None.
Tests to add
Preprocessor(policy={"missing": "error"}).fit(X)raisesInvalidParamErrornamingmissingand listingconstant/out_of_range.RepresentationPolicy.resolve({"out_of_range": "clip"})still works (guard).
Documentation to update
Preprocessordocstring (policy), as in the sketch. Optionallydocs/representations/choosing_a_method.md, which already saysout_of_rangeis "not yet available throughPreprocessor".
Acceptance criteria
- Unknown policy keys raise
InvalidParamErroron every path. - The facade docstring describes only what is applied.
-
just test,just docsandjust checkpass; the change follows the documented dependency direction.
Related issues
#183 (open, PR #184) changes how the same policy names constant columns; the two changes touch different functions.
Activity