Skip to content

[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
mainfrom
repo-assist/fix-gcm-falsify-matplotlib-guard-20260728-bca1f2c95c638afe
Draft

[Repo Assist] fix(gcm/falsify): guard matplotlib import — prevents ModuleNotFoundError when importing falsify without [plotting]#1718
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/fix-gcm-falsify-matplotlib-guard-20260728-bca1f2c95c638afe

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This is an automated PR from Repo Assist, an AI assistant for this repository.

Problem

gcm/falsify.py had two module-level matplotlib imports:

import matplotlib.colors as mcolors
import matplotlib.pyplot as plt

and a module-level constant that evaluated them immediately:

COLORS = list(mcolors.TABLEAU_COLORS.values())

This meant that merely importing dowhy.gcm.falsify (or any code path that loaded the module) would fail with ModuleNotFoundError: 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 in falsify.py that uses matplotlib directly. plot_local_insights() calls plot() from dowhy.gcm.util, which is already guarded by the companion PR #[task4-pr].

Fix

  • Remove the two top-level matplotlib imports
  • Remove COLORS = list(mcolors.TABLEAU_COLORS.values()) from module scope
  • Add a lazy try/except guard + local colors list at the start of plot_evaluation_results():
try:
    import matplotlib.colors as mcolors
    import matplotlib.pyplot as plt
except ImportError:
    raise ImportError(
        "matplotlib is required for plotting. Install it with: pip install 'dowhy[plotting]'"
    )

colors = list(mcolors.TABLEAU_COLORS.values())

All COLORS[...] references inside the function are updated to colors[...].

VIOLATION_COLOR = "red" is a plain string constant and is unaffected.

Result

  • import dowhy.gcm.falsify no longer requires matplotlib
  • Calling plot_evaluation_results() without matplotlib gives a clear, actionable error message instead of a confusing ModuleNotFoundError

Test Status

Only import restructuring — no logic changed.
flake8 --select=E9,F63,F7,F82 passes (0 hard errors).

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

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>
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.

0 participants