[Repo Assist] improve: replace generic Exception with ValueError in causal estimators#1532
Open
github-actions[bot] wants to merge 9 commits into
Open
Conversation
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>
Contributor
There was a problem hiding this comment.
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(...)withraise 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.
57 tasks
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>
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
…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>
Contributor
Author
|
Commit pushed:
|
…ators-1c15f47dfb600b6e
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>
…ators-1c15f47dfb600b6e
…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>
Contributor
Author
|
Commit pushed:
|
41 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated improvement from Repo Assist.
Summary
All input-validation errors in four estimators were raising the base
Exceptionclass instead of the more specificValueError. This change replaces them withValueErrorthroughout.Why This Matters
Using
ValueErrorfor invalid-input scenarios is the standard Python convention ([PEP 8 / builtins docs]((docs.python.org/redacted) Raising the baseExceptionclass:except ValueError: ...ValueErroris already used for similar validation errors (e.g. inregression_estimator.py,generalized_linear_model_estimator.py,instrumental_variable_estimator.py)Change
raise Exception(msg)→raise ValueError(msg)in all input-validation checks across:doubly_robust_estimator.pypropensity_score_estimator.pydistance_matching_estimator.pytwo_stage_regression_estimator.py__init__andfit)Also tightens the
pytest.raises((ValueError, Exception))intest_two_stage_regression_estimator.pyto justpytest.raises(ValueError).Backwards Compatibility
ValueErroris a subclass ofException, so existing code usingexcept Exception:continues to work unchanged. This is a non-breaking improvement.Test Status
All 10 tests in
test_doubly_robust_estimator.pyandtest_two_stage_regression_estimator.pypass: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).