|
4 | 4 | Extracts ``.. code-block:: python`` snippets from RST files and executes them |
5 | 5 | in isolated namespaces with synthetic data and mock dataset loaders. Fails on |
6 | 6 | 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). |
8 | 9 | """ |
9 | 10 |
|
10 | 11 | import re |
@@ -104,7 +105,7 @@ def _extract_snippets(rst_path: Path) -> List[Tuple[int, str]]: |
104 | 105 | # Third-party packages imported by comparison-page snippets that may not |
105 | 106 | # be installed in the test environment. Only these are exempt from |
106 | 107 | # ImportError failures — diff_diff and stdlib imports must succeed. |
107 | | -_THIRD_PARTY_MODULES = {"pyfixest", "linearmodels", "differences"} |
| 108 | +_THIRD_PARTY_MODULES = {"pyfixest", "linearmodels", "differences", "matplotlib"} |
108 | 109 |
|
109 | 110 |
|
110 | 111 | def _should_skip(code: str) -> Optional[str]: |
@@ -362,11 +363,17 @@ def test_doc_snippet(test_id: str, code: str, skip_reason: Optional[str]): |
362 | 363 | pass |
363 | 364 | except ImportError as exc: |
364 | 365 | # 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. |
367 | 370 | mod_name = getattr(exc, "name", "") or "" |
368 | 371 | 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: |
370 | 377 | pytest.fail( |
371 | 378 | f"Snippet {test_id} raised ImportError for " |
372 | 379 | f"'{mod_name}': {exc}\n\n" |
|
0 commit comments