[Repo Assist] fix(gcm/falsify): guard matplotlib import — prevents ModuleNotFoundError when importing falsify without [plotting] - #1718
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
import matplotlib was at module level, so `import dowhy.gcm.falsify` or any import of the module-level COLORS constant failed with ModuleNotFoundError when matplotlib is not installed — even for users who never call any plotting function. Changes: - Remove top-level `import matplotlib.colors as mcolors` and `import matplotlib.pyplot as plt` - Remove module-level `COLORS = list(mcolors.TABLEAU_COLORS.values())` - Add lazy try/except import + local `colors` definition inside `plot_evaluation_results()`, the only function that uses them `VIOLATION_COLOR = "red"` (a plain string) is unaffected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This was referenced Jul 28, 2026
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 PR from Repo Assist, an AI assistant for this repository.
Problem
gcm/falsify.pyhad two module-level matplotlib imports:and a module-level constant that evaluated them immediately:
This meant that merely importing
dowhy.gcm.falsify(or any code path that loaded the module) would fail withModuleNotFoundError: No module named 'matplotlib'in environments without the[plotting]extra, even when the caller had no intention of plotting anything.plot_evaluation_results()is the only function infalsify.pythat usesmatplotlibdirectly.plot_local_insights()callsplot()fromdowhy.gcm.util, which is already guarded by the companion PR #[task4-pr].Fix
COLORS = list(mcolors.TABLEAU_COLORS.values())from module scopetry/exceptguard + localcolorslist at the start ofplot_evaluation_results():All
COLORS[...]references inside the function are updated tocolors[...].VIOLATION_COLOR = "red"is a plain string constant and is unaffected.Result
import dowhy.gcm.falsifyno longer requires matplotlibplot_evaluation_results()without matplotlib gives a clear, actionable error message instead of a confusingModuleNotFoundErrorTest Status
Only import restructuring — no logic changed.
✅
flake8 --select=E9,F63,F7,F82passes (0 hard errors).