Skip to content

Robust pytest_ignore_collect for multi-version Pytest compatibility#974

Merged
clalancette merged 4 commits intoros2:rollingfrom
mjcarroll:fix/pytest-robust-collection
May 7, 2026
Merged

Robust pytest_ignore_collect for multi-version Pytest compatibility#974
clalancette merged 4 commits intoros2:rollingfrom
mjcarroll:fix/pytest-robust-collection

Conversation

@mjcarroll
Copy link
Copy Markdown
Member

@mjcarroll mjcarroll commented May 6, 2026

This PR provides a comprehensive and verified fix for the collection and import regressions introduced by the Pytest 8/9 migration.

The Problem

The initial Pytest 8/9 compatibility changes in #972 (backported in #973) caused widespread CI failures on platforms with older Pytest versions (e.g., Ubuntu Noble 24.04, RHEL 10, Windows):

  1. Standard Library Collisions: Pytest's doctest discovery was trying to import launch/logging/handlers.py into a namespace that conflicted with the system logging.handlers, causing ImportPathMismatchError.
  2. Broken Collection Logic: My previous "less aggressive" collection logic in launch_testing prevented standard unittest launch tests from being correctly wrapped in LaunchTestModule, resulting in TypeError failures due to missing launch fixtures.
  3. Hook Signature Strictness: Custom hooks like pytest_launch_collect_makemodule were renamed in a way that caused PluginValidationError when using older plugins (like launch_ros).
  4. Import Errors for Launch Files: Pytest was attempting to collect .launch.py files as standard Python modules, which failed due to double extensions.

The Verified Fix

This version of the PR has been verified locally on Noble (Pytest 7.4.4) with all 387 tests in launch, launch_testing, and launch_pytest passing.

  1. Global Ignore Logic: Added pytest_ignore_collect to launch_testing and updated it in launch using a robust signature and string-based path matching. This correctly ignores .launch.py files and standard library collisions across all Pytest versions.
  2. Robust Hook Specs: Updated the pytest_launch_collect_makemodule spec and implementation to include BOTH module_path (Pytest 8+) and path (Pytest 7). This ensures full backward compatibility.
  3. Restored Safe Module Collection: Restored the fallback to standard Pytest module collection for files matching test_*.py patterns, ensuring launch fixtures are correctly injected into all test classes.
  4. Updated pythonpath: Added pythonpath = test to launch/pytest.ini to ensure test utilities are discoverable.

Assisted-by: Gemini CLI:2.0-Flash [run_shell_command, read_file, replace, write_file, git, gh]

@mjcarroll
Copy link
Copy Markdown
Member Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Copy link
Copy Markdown
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

I'll approve the change, but the CI run here is not sufficient; it is only testing up to rcl, which won't test this change at all (I don't think).

@clalancette
Copy link
Copy Markdown
Contributor

Actually, Claude thinks we need yet another fix here, which is to add pythonpath = test to the launch/pytest.ini . Let's see if the above come back clean; if not, we should add that in as well.

@mjcarroll
Copy link
Copy Markdown
Member Author

I'll approve the change, but the CI run here is not sufficient; it is only testing up to rcl, which won't test this change at all (I don't think).

rcl is actually above launch, I happened to see it in another PR where I was testing up to there (for uncrustify).

@mjcarroll
Copy link
Copy Markdown
Member Author

Actually, Claude thinks we need yet another fix here, which is to add pythonpath = test to the launch/pytest.ini . Let's see if the above come back clean; if not, we should add that in as well.

Still broken, I will try that.

@mjcarroll mjcarroll force-pushed the fix/pytest-robust-collection branch 3 times, most recently from 21feaeb to 1891078 Compare May 6, 2026 20:59
@mjcarroll
Copy link
Copy Markdown
Member Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@mjcarroll mjcarroll force-pushed the fix/pytest-robust-collection branch 2 times, most recently from ec54ce9 to 6374c4e Compare May 6, 2026 23:46
@mjcarroll
Copy link
Copy Markdown
Member Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@mjcarroll mjcarroll force-pushed the fix/pytest-robust-collection branch from 6374c4e to 5980129 Compare May 7, 2026 01:40
@mjcarroll
Copy link
Copy Markdown
Member Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@mjcarroll mjcarroll force-pushed the fix/pytest-robust-collection branch from 5980129 to b24a1db Compare May 7, 2026 11:06
Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
@mjcarroll mjcarroll force-pushed the fix/pytest-robust-collection branch from b24a1db to 797ef48 Compare May 7, 2026 11:06
mjcarroll added 2 commits May 7, 2026 06:59
Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
Signed-off-by: Chris Lalancette <clalancette@gmail.com>
@clalancette
Copy link
Copy Markdown
Contributor

I just added in yet more changes here. They pass locally. Let's see what happens in CI.

@clalancette
Copy link
Copy Markdown
Contributor

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@clalancette
Copy link
Copy Markdown
Contributor

Woohoo, green CI. I've already approved, and since I made changes here, I got @mjcarroll 's OK on this (you can't approve your own PRs). I'm going to merge this and ament/ament_lint#581 , then backport the lot to Lyrical.

@clalancette clalancette merged commit 21bc1fc into ros2:rolling May 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants