Skip to content

fix: restore example collection during directory traversal (#794) - #795

Merged
planetf1 merged 5 commits into
generative-computing:mainfrom
planetf1:fix/example-collection-794
Apr 9, 2026
Merged

planetf1 merged 5 commits into
generative-computing:mainfrom
planetf1:fix/example-collection-794

Conversation

@planetf1

@planetf1 planetf1 commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

Misc PR

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

#794 — directory collection broken: pytest_collect_file was returning None for non-skipped examples, relying on pytest_pycollect_makemodule to handle collection. That hook only fires for files matching python_files (test_*.py), so examples were silently dropped during directory traversal. Fixed by returning ExampleModule from pytest_collect_file.

#796 — __init__.py and helpers.py incorrectly collected: Once collection was restored, support files without a # pytest: marker were being collected as tests. Switched to opt-in — examples now require an explicit # pytest: comment to be collected. Files without one are silently ignored. Removed the spurious marker from helper/helpers.py and documented the opt-in behaviour in AGENTS.md and MARKERS_GUIDE.md.

Hardening (code review follow-up):

  • Fix duplicate collection for directly-specified files (isinitpath guard)
  • Remove dead except OSError in pytest_collect_file
  • Key examples_to_skip by full path instead of basename (prevents collision across subdirectories)
  • Use file_path.parts instead of str() substring match in pytest_pycollect_makemodule
  • Add test/test_example_collection.py regression test

CONTRIBUTING.md — expanded required Ollama models list: The previous list only covered 4 models (CI subset). Expanded to include all models needed by examples and the test suite, grouped by context with a one-liner to pull the lot.

Discovered during investigation

Running the full example suite after fixing collection surfaced two further issues:

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

…e-computing#794)

pytest_pycollect_makemodule only fires for files matching python_files
(test_*.py) — return ExampleModule from pytest_collect_file so directory
traversal collects examples.
@github-actions github-actions Bot added the bug Something isn't working label Apr 8, 2026
@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

The PR description has been updated. Please fill out the template for your PR to be reviewed.

…uting#796)

Examples without a marker are now silently ignored instead of being
collected as tests. Removes spurious marker from helper/helpers.py
and documents opt-in behaviour in AGENTS.md and MARKERS_GUIDE.md.
@planetf1

planetf1 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Example output:

➜ uv run pytest docs/examples --collect-only
====================================================================== test session starts ======================================================================
platform darwin -- Python 3.12.8, pytest-9.0.2, pluggy-1.6.0
rootdir: /Users/jonesn/src/mellea
configfile: pyproject.toml
plugins: nbmake-1.5.5, recording-0.13.4, cov-7.1.0, xdist-3.8.0, json-report-1.5.0, timeout-2.4.0, metadata-3.1.1, asyncio-1.3.0, Faker-40.12.0, langsmith-0.7.24, anyio-4.13.0
timeout: 900.0s
timeout method: signal
timeout func_only: False
asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 83 items / 4 deselected / 79 selected

<Dir mellea>
  <Package docs>
    <Package examples>
      <Dir aLora>
        <ExampleModule 101_example.py>
          <ExampleItem 101_example.py>
      <Dir agents>
        <Dir react>
          <Dir react_from_scratch>
            <ExampleModule react.py>
              <ExampleItem react.py>
            <ExampleModule react_instruct.py>
              <ExampleItem react_instruct.py>
...... (continues)

======================================================================= Skipped Examples ========================================================================
The following examples were skipped during collection:

  • 102_example.py: Example marked with skip marker
  • example_readme_generator.py: Example marked with skip marker
  • make_training_data.py: Example marked with skip marker
 .... (continues : these will only list those explicitly marked as skipped)
======================================================================== tests coverage =========================================================================
_______________________________________________________ coverage: platform darwin, python 3.12.8-final-0 ________________________________________________________

Coverage HTML written to dir htmlcov
Coverage JSON written to file coverage.json
========================================================= 79/83 tests collected (4 deselected) in 9.63s =========================================================


The previous list only covered 4 models (CI subset). Expanded to include
all models needed by examples and tests, grouped by context (CI, examples,
test suite) with a one-liner to pull the lot.
@planetf1

planetf1 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Following this fix I was able to collect and run all docs/examples -- Only 2 sofia tests failed locally , which were due to resource exhaustion (ollama models loaded like granite guardian, then no space for additional ones on a 32GB system) and will be addressed by the referenced issues.

- pytest_pycollect_makemodule: return SkippedFile (not None) for
  markerless files, closing the direct-specification bypass
- pytest_collect_file: narrow except to OSError, return None on
  failure (exclude) instead of falling through to collect
- Fix stale docstring and comments from the old collection model

@jakelorocco jakelorocco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you; I like this opt-in approach

Address code review findings from PR generative-computing#795:

- Fix duplicate collection: guard pytest_collect_file with isinitpath()
  so directly-specified files defer to pytest_pycollect_makemodule
- Remove dead try/except OSError (_extract_markers_from_file is
  self-contained)
- Key examples_to_skip by full path instead of basename to prevent
  collisions across subdirectories
- Use file_path.parts instead of str() substring match in
  pytest_pycollect_makemodule for correct path filtering
- Add test/test_example_collection.py regression test verifying
  support files are excluded, examples are collected, and no
  duplicates occur

Signed-off-by: Nigel Jones <[email protected]>
Signed-off-by: Nigel Jones <[email protected]>
@planetf1

planetf1 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Additional hardening (commits 5–6)

Code review surfaced several issues in the collection hooks we modified. All fixed in 03c684e:

Fixes

  • Duplicate collection for directly-specified files — both pytest_collect_file and pytest_pycollect_makemodule were returning ExampleModule for files specified on the command line (e.g. pytest docs/examples/hello_world.py), causing each example to run twice. Added an isinitpath() guard so pytest_collect_file defers to pytest_pycollect_makemodule for direct specification.

  • Dead except OSError — _extract_markers_from_file catches all exceptions internally and returns [], so the outer try/except OSError in pytest_collect_file was unreachable. Removed the dead block.

  • examples_to_skip keyed by basename — if two examples in different subdirectories shared a filename, one skip status would clobber the other. Keyed by full path instead.

  • str(file_path) substring match — pytest_pycollect_makemodule used "docs" not in str(file_path) which could match paths containing "docs" as a substring (e.g. test/test_docs_foo.py). Changed to file_path.parts to match the other hooks.

Regression test

Added test/test_example_collection.py — runs pytest --collect-only docs/examples/ and asserts:

  • __init__.py, helpers.py, conftest.py never collected
  • At least 50 examples collected (sanity floor)
  • No duplicates

Verification

Scenario Result
Everything (test/ + docs/examples/) 1079 collected
Tests only (test/) 1000 collected
Examples only (docs/examples/) 79 collected (4 deselected)
Single example directly (hello_world.py) 1 collected, no duplicate
Two examples directly (sofai + guardian) 2 collected, no duplicates
helpers.py directly 0 collected (correct — no marker)
__init__.py directly 0 collected (correct — no marker)
Regression test PASSED
Pre-commit hooks All passed

@planetf1
planetf1 marked this pull request as ready for review April 9, 2026 13:41
@planetf1
planetf1 requested a review from a team as a code owner April 9, 2026 13:41
@planetf1

planetf1 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@jakelorocco thanks for the review - rerequesting as I layered on additional changes affecting collection.

@planetf1
planetf1 enabled auto-merge April 9, 2026 13:42
@planetf1
planetf1 added this pull request to the merge queue Apr 9, 2026
Merged via the queue into generative-computing:main with commit da53bb0 Apr 9, 2026
8 checks passed
@planetf1
planetf1 deleted the fix/example-collection-794 branch April 9, 2026 14:51
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request May 27, 2026
…e-computing#794) (generative-computing#795)

* fix: restore example collection during directory traversal (generative-computing#794)

pytest_pycollect_makemodule only fires for files matching python_files
(test_*.py) — return ExampleModule from pytest_collect_file so directory
traversal collects examples.

* fix: require # pytest: marker for example collection (generative-computing#796)

Examples without a marker are now silently ignored instead of being
collected as tests. Removes spurious marker from helper/helpers.py
and documents opt-in behaviour in AGENTS.md and MARKERS_GUIDE.md.

* docs: expand required Ollama models list in CONTRIBUTING.md

The previous list only covered 4 models (CI subset). Expanded to include
all models needed by examples and tests, grouped by context (CI, examples,
test suite) with a one-liner to pull the lot.

* fix: harden example collection hooks against silent failures

- pytest_pycollect_makemodule: return SkippedFile (not None) for
  markerless files, closing the direct-specification bypass
- pytest_collect_file: narrow except to OSError, return None on
  failure (exclude) instead of falling through to collect
- Fix stale docstring and comments from the old collection model

* fix: harden collection hooks and add regression test

Address code review findings from PR generative-computing#795:

- Fix duplicate collection: guard pytest_collect_file with isinitpath()
  so directly-specified files defer to pytest_pycollect_makemodule
- Remove dead try/except OSError (_extract_markers_from_file is
  self-contained)
- Key examples_to_skip by full path instead of basename to prevent
  collisions across subdirectories
- Use file_path.parts instead of str() substring match in
  pytest_pycollect_makemodule for correct path filtering
- Add test/test_example_collection.py regression test verifying
  support files are excluded, examples are collected, and no
  duplicates occur

Signed-off-by: Nigel Jones <[email protected]>
Signed-off-by: Nigel Jones <[email protected]>

---------

Signed-off-by: Nigel Jones <[email protected]>
Signed-off-by: Nigel Jones <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

2 participants