Skip to content

fix(preprocessor): report unknown columns when labels mix int and str - #170

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/169-mixed-label-error-messages
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/169-mixed-label-error-messages

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #169.

With mixed int/str column labels, a feature_preprocessing key or FeatureGroup source that names a missing column raised TypeError: '<' not supported between instances of 'int' and 'str' instead of the actionable InvalidParamError.

Root cause

The message builders sorted labels that are not mutually comparable:

  • invalid_param_error (pretab/exceptions.py) called sorted(valid), and both the override check and validate_feature_groups pass the frame's columns as valid;
  • Preprocessor._validate_feature_overrides also called sorted(unknown_features).

Fix

  • New private helper _sorted_for_display in pretab/exceptions.py: it returns sorted(values), or, when that raises TypeError, the values ordered by their string form (with the type name as a tie-breaker).
  • invalid_param_error uses it for the valid values, and _validate_feature_overrides uses it for the unknown keys.
  • Comparable values keep their natural order, for example [1, 2, 10], so existing messages are unchanged.

Behaviour changes

  • A misspelled column with mixed labels now raises InvalidParamError listing e.g. Valid values: [0, 1, 'age']. Before, it raised TypeError.
  • Messages for homogeneous labels and all other parameters are unchanged.

Documentation

  • invalid_param_error docstring: how incomparable valid values are listed.
  • No user-facing page documents this message.

Tests

  • tests/integration/test_exceptions.py::test_invalid_param_error_lists_mixed_type_valid_values: fails on main, passes here.
  • tests/integration/test_exceptions.py::test_invalid_param_error_keeps_the_natural_order_of_comparable_values: guard that passes on both.
  • tests/integration/test_column_labels.py::test_unknown_column_with_mixed_labels_raises_invalid_param_error (override, two overrides, group source): fails on main, passes here.

Reproduction

The script from #169 on this branch:

columns: ['age', 0, 1] | valid override fits: {'age': 7, 0: 7, 1: 7}
feature_preprocessing typo: InvalidParamError: Preprocessor.feature_preprocessing = ['agee'] is invalid.
feature_groups typo: InvalidParamError: feature_groups['pair'].columns[1] = 3 is invalid.

Checks

  • just test: 4681 passed, 59 skipped, 7 xfailed; coverage 95.71% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4498 passed, 173 skipped, 7 xfailed; scripts/quickstart.py passes.
  • just docs: strict build succeeds.
  • just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).
  • The facade imports the helper from pretab.exceptions, the shared leaf module; no import direction changes.

Merge notes

invalid_param_error sorted the valid values and _validate_feature_overrides
sorted the unknown keys for the message. With mixed int/str column labels,
which PreTab supports, sorted() raised a TypeError, so a misspelled
feature_preprocessing key or FeatureGroup source surfaced as an unrelated
error instead of the actionable InvalidParamError.

A new _sorted_for_display helper sorts normally and falls back to the
string form for values that cannot be compared. Both message paths use it.

Fixes #169

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A misspelled column key raises TypeError instead of InvalidParamError when column labels mix int and str

1 participant