Skip to content

Commit 1ac1788

Browse files
igerberclaude
andcommitted
Add matplotlib to known third-party modules in doc snippet tests
The visualization module raises ImportError with a message (not exc.name) when matplotlib is missing. Also match against the error message string so optional-dependency guards are correctly suppressed in Pure Python CI. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 21fd61a commit 1ac1788

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

tests/test_doc_snippets.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Extracts ``.. code-block:: python`` snippets from RST files and executes them
55
in isolated namespaces with synthetic data and mock dataset loaders. Fails on
66
all exceptions except NameError (context-dependent snippets) and
7-
ImportError for known third-party packages (comparison-page snippets).
7+
ImportError for known third-party/optional packages (comparison-page
8+
snippets and optional-dependency guards like matplotlib).
89
"""
910

1011
import re
@@ -104,7 +105,7 @@ def _extract_snippets(rst_path: Path) -> List[Tuple[int, str]]:
104105
# Third-party packages imported by comparison-page snippets that may not
105106
# be installed in the test environment. Only these are exempt from
106107
# ImportError failures — diff_diff and stdlib imports must succeed.
107-
_THIRD_PARTY_MODULES = {"pyfixest", "linearmodels", "differences"}
108+
_THIRD_PARTY_MODULES = {"pyfixest", "linearmodels", "differences", "matplotlib"}
108109

109110

110111
def _should_skip(code: str) -> Optional[str]:
@@ -362,11 +363,17 @@ def test_doc_snippet(test_id: str, code: str, skip_reason: Optional[str]):
362363
pass
363364
except ImportError as exc:
364365
# Only suppress ImportError for known third-party packages that
365-
# comparison-page snippets import. In-package (diff_diff.*) and
366-
# stdlib import failures should still fail the test.
366+
# comparison-page snippets import (or optional-dependency guards
367+
# that raise ImportError manually with the package name in the
368+
# message). In-package (diff_diff.*) and stdlib import failures
369+
# should still fail the test.
367370
mod_name = getattr(exc, "name", "") or ""
368371
top_level = mod_name.split(".")[0]
369-
if top_level not in _THIRD_PARTY_MODULES:
372+
msg = str(exc).lower()
373+
is_known = top_level in _THIRD_PARTY_MODULES or any(
374+
pkg in msg for pkg in _THIRD_PARTY_MODULES
375+
)
376+
if not is_known:
370377
pytest.fail(
371378
f"Snippet {test_id} raised ImportError for "
372379
f"'{mod_name}': {exc}\n\n"

0 commit comments

Comments
 (0)