Skip to content

[Repo Assist] improve: replace generic Exception with ValueError in causal estimators#1532

Open
github-actions[bot] wants to merge 9 commits into
mainfrom
repo-assist/improve-exception-types-in-estimators-1c15f47dfb600b6e
Open

[Repo Assist] improve: replace generic Exception with ValueError in causal estimators#1532
github-actions[bot] wants to merge 9 commits into
mainfrom
repo-assist/improve-exception-types-in-estimators-1c15f47dfb600b6e

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated improvement from Repo Assist.

Summary

All input-validation errors in four estimators were raising the base Exception class instead of the more specific ValueError. This change replaces them with ValueError throughout.

Why This Matters

Using ValueError for invalid-input scenarios is the standard Python convention ([PEP 8 / builtins docs]((docs.python.org/redacted) Raising the base Exception class:

  • Makes error handling harder for callers — they can't selectively catch except ValueError: ...
  • Is inconsistent with the rest of the codebase, where ValueError is already used for similar validation errors (e.g. in regression_estimator.py, generalized_linear_model_estimator.py, instrumental_variable_estimator.py)
  • Is harder to distinguish from unexpected runtime errors

Change

raise Exception(msg)raise ValueError(msg) in all input-validation checks across:

File Sites
doubly_robust_estimator.py 3 (too many treatments, too many outcomes, wrong identifier method)
propensity_score_estimator.py 3 (no confounders, too many treatments, non-binary treatment)
distance_matching_estimator.py 3 (too many treatments, non-binary treatment, no confounders)
two_stage_regression_estimator.py 2 (too many treatments — in __init__ and fit)

Also tightens the pytest.raises((ValueError, Exception)) in test_two_stage_regression_estimator.py to just pytest.raises(ValueError).

Backwards Compatibility

ValueError is a subclass of Exception, so existing code using except Exception: continues to work unchanged. This is a non-breaking improvement.

Test Status

All 10 tests in test_doubly_robust_estimator.py and test_two_stage_regression_estimator.py pass:

tests/causal_estimators/test_doubly_robust_estimator.py ....         [4 tests]
tests/causal_estimators/test_two_stage_regression_estimator.py ...... [6 tests]
10 passed in 36.42s

Format (black --fast), import order (isort), and lint (flake8) all clean on the changed files (pre-existing E501/C901 issues in unchanged lines are not introduced by this PR).

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@11c9a2c442e519ff2b427bf58679f5a525353f76

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@11c9a2c442e519ff2b427bf58679f5a525353f76

All input-validation errors in the following estimators were raising
the base Exception class instead of the more specific ValueError.
ValueError is the conventional Python choice for invalid argument or
state errors, it is a subclass of Exception (fully backwards compatible),
and lets callers catch it selectively with 'except ValueError'.

Files changed:
- dowhy/causal_estimators/doubly_robust_estimator.py (3 sites)
- dowhy/causal_estimators/propensity_score_estimator.py (3 sites)
- dowhy/causal_estimators/distance_matching_estimator.py (3 sites)
- dowhy/causal_estimators/two_stage_regression_estimator.py (2 sites)
- tests/causal_estimators/test_two_stage_regression_estimator.py:
  tighten pytest.raises from (ValueError, Exception) to ValueError

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR standardizes input-validation error handling in several causal estimators by replacing generic Exception raises with ValueError, aligning with Python conventions and making it easier for callers to catch invalid-input failures distinctly from unexpected runtime errors.

Changes:

  • Replaced raise Exception(...) with raise ValueError(...) for input-validation checks across four estimator implementations.
  • Tightened a unit test to expect ValueError (instead of (ValueError, Exception)) for invalid frontdoor variable configurations.
  • Minor formatting cleanup in multi-line string .replace(...) calls in the test file.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
dowhy/causal_estimators/two_stage_regression_estimator.py Uses ValueError for invalid treatment dimensionality checks in __init__ and fit.
dowhy/causal_estimators/propensity_score_estimator.py Uses ValueError for missing confounders, multi-treatment, and non-binary treatment validation.
dowhy/causal_estimators/doubly_robust_estimator.py Uses ValueError for invalid estimand configuration (treatment/outcome dimensionality and identifier method).
dowhy/causal_estimators/distance_matching_estimator.py Uses ValueError for multi-treatment, non-binary treatment, and missing confounders validation.
tests/causal_estimators/test_two_stage_regression_estimator.py Updates expectation to ValueError and reformats graph-string .replace(...) calls.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread dowhy/causal_estimators/two_stage_regression_estimator.py Outdated
Comment thread dowhy/causal_estimators/two_stage_regression_estimator.py Outdated
Comment thread dowhy/causal_estimators/propensity_score_estimator.py Outdated
Comment thread dowhy/causal_estimators/distance_matching_estimator.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Emre Kıcıman <emrek@microsoft.com>
emrekiciman and others added 3 commits May 20, 2026 21:44
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Emre Kıcıman <emrek@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Emre Kıcıman <emrek@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Emre Kıcıman <emrek@microsoft.com>
Copilot stopped work on behalf of emrekiciman due to an error May 21, 2026 04:45
Copilot AI requested a review from emrekiciman May 21, 2026 04:45
…mator

Adds a test verifying that DoublyRobustEstimator raises ValueError
when called with more than one treatment variable. Complementary to
the Exception->ValueError changes in the main PR.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor Author

Commit pushed: e718647

Generated by 🌈 Repo Assist, see workflow run. Learn more.

github-actions Bot added a commit that referenced this pull request May 26, 2026
…emaining modules

Replace bare `raise Exception` with semantically correct specific types
across 8 modules not covered by PR #1532:

- ValueError: wrong argument value, unrecognized variable type, invalid graph structure
- TypeError: unrecognized input format/type
- NotImplementedError: abstract method that subclasses must override

Files changed:
- dowhy/causal_identifier/id_identifier.py
- dowhy/causal_prediction/algorithms/base_algorithm.py
- dowhy/do_samplers/kernel_density_sampler.py
- dowhy/do_samplers/mcmc_sampler.py
- dowhy/api/causal_data_frame.py
- dowhy/utils/api.py
- dowhy/utils/propensity_score.py
- dowhy/gcm/equation_parser.py

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
emrekiciman and others added 2 commits May 25, 2026 19:01
…o match CI black version

The CI black version formats .replace() differently to the version used
when creating the PR. Revert to the multiline format that CI expects.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor Author

Commit pushed: c886f3c

Generated by 🌈 Repo Assist, see workflow run. Learn more.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants