From b019af6cc79f12e433932eaa56914116b0b41f4a Mon Sep 17 00:00:00 2001 From: shauryagangrade <288927048+shauryagangrade@users.noreply.github.com> Date: Thu, 3 Sep 2026 22:52:56 +0530 Subject: [PATCH 1/3] fix: raise ValueError for unrecognized strategy names in strategy_features() strategy_features() silently accepted any unrecognized strategy name (e.g. a typo like 'in_procesing') by falling through to the bare 'return list(core)' branch. This gave a wrong-but-plausible result with no error, making bugs hard to trace. Replace the catch-all return with an explicit check for the three known core-only strategies, and raise ValueError for anything else. Add tests covering both a typo'd strategy name and an empty string. Fixes #418 --- faircode/strategies.py | 4 +++- tests/test_strategies.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/faircode/strategies.py b/faircode/strategies.py index 2ec9342..6e8662f 100644 --- a/faircode/strategies.py +++ b/faircode/strategies.py @@ -99,7 +99,9 @@ def strategy_features(strategy: str, core: list, proxies: list, protected: list) return list(dict.fromkeys(core + proxies + protected)) if strategy == "unawareness": return list(dict.fromkeys(core + proxies)) - return list(core) # unawareness_proxy_removal, in_processing, post_processing + if strategy in ("unawareness_proxy_removal", "in_processing", "post_processing"): + return list(core) + raise ValueError(f"unknown strategy: {strategy!r}") def fit_in_processing(base_model, X_train, y_train, sensitive_train): diff --git a/tests/test_strategies.py b/tests/test_strategies.py index 58178e1..d89b721 100644 --- a/tests/test_strategies.py +++ b/tests/test_strategies.py @@ -65,6 +65,16 @@ def test_strategy_features_deduplicates_overlapping_names(): assert set(cols) == {"a", "b", "c"} +def test_strategy_features_raises_on_unknown_strategy(): + with pytest.raises(ValueError, match="unknown strategy: 'in_procesing'"): + strategy_features("in_procesing", CORE, PROXIES, PROTECTED) + + +def test_strategy_features_raises_on_empty_string(): + with pytest.raises(ValueError, match="unknown strategy: ''"): + strategy_features("", CORE, PROXIES, PROTECTED) + + # ── encode_features ────────────────────────────────────────────────────────── def test_encode_features_passes_numeric_columns_through(): df = pd.DataFrame({"age": [20.0, 30.0, 40.0]}) From 00183df396ed845a41c876d7ef7cb1b29a9f2d29 Mon Sep 17 00:00:00 2001 From: Yash Kewlani <86704881+yakew7@users.noreply.github.com> Date: Thu, 3 Sep 2026 22:58:05 +0530 Subject: [PATCH 2/3] Fix strategy handling for unawareness_proxy_removal Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- faircode/strategies.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/faircode/strategies.py b/faircode/strategies.py index 6e8662f..593ced1 100644 --- a/faircode/strategies.py +++ b/faircode/strategies.py @@ -99,8 +99,8 @@ def strategy_features(strategy: str, core: list, proxies: list, protected: list) return list(dict.fromkeys(core + proxies + protected)) if strategy == "unawareness": return list(dict.fromkeys(core + proxies)) - if strategy in ("unawareness_proxy_removal", "in_processing", "post_processing"): - return list(core) +if strategy in ("unawareness_proxy_removal", "in_processing", "post_processing"): + return list(dict.fromkeys(core)) raise ValueError(f"unknown strategy: {strategy!r}") From 9e6b7cc8ff32ea8513f11d08f3580441763c40a3 Mon Sep 17 00:00:00 2001 From: Yash Kewlani <86704881+yakew7@users.noreply.github.com> Date: Thu, 3 Sep 2026 23:02:45 +0530 Subject: [PATCH 3/3] Fix indentation for strategy condition check Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- faircode/strategies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faircode/strategies.py b/faircode/strategies.py index 593ced1..c47b189 100644 --- a/faircode/strategies.py +++ b/faircode/strategies.py @@ -99,7 +99,7 @@ def strategy_features(strategy: str, core: list, proxies: list, protected: list) return list(dict.fromkeys(core + proxies + protected)) if strategy == "unawareness": return list(dict.fromkeys(core + proxies)) -if strategy in ("unawareness_proxy_removal", "in_processing", "post_processing"): + if strategy in ("unawareness_proxy_removal", "in_processing", "post_processing"): return list(dict.fromkeys(core)) raise ValueError(f"unknown strategy: {strategy!r}")