Skip to content

Preserve '.[' in long report headlines (getmodpath sanitization) #22

@rowan-stein

Description

@rowan-stein

Summary

  • Parametrized test ids containing the substring ".[" are incorrectly mutated in long report output/headlines: any ".[" becomes "[". Example: "..[" becomes ".[" in the failure header. This also affects tools relying on headlines (e.g., VS Code test discovery heuristics).

Observed failure

bug.py F                                                                 [100%]

=================================== FAILURES ===================================
_________________________________ test_boo[.[] _________________________________

a = '..['

    @pytest.mark.parametrize("a",["..["])
    def test_boo(a):
>       assert 0
E       assert 0

bug.py:6: AssertionError
============================== 1 failed in 0.06s ===============================

The "test_boo[..[]" is rendered as "test_boo[.[]" in the failure headline.

Root cause

  • src/_pytest/python.py: in PyobjMixin.getmodpath(), the result is returned as s.replace(".[", "[").
  • This was historically intended to avoid dotted paths like test_gen.[0] from yield-based tests (removed in pytest 4.0).
  • With yield tests gone, this sanitization is obsolete and now corrupts parameter ids which legitimately contain the sequence ".[".

Proposed fix

  • Change getmodpath() to return s unchanged, and add a brief comment noting yield tests removal.

Reproduction steps

  1. Create bug.py:
import pytest

@pytest.mark.parametrize("a", ["..["])
def test_boo(a):
    assert False
  1. Run: pytest -vv bug.py.
  2. Observe the failure headline shows test_boo[.[] instead of test_boo[..[].

Acceptance criteria

  • Long report failure headlines preserve the exact parameter id text; no mutation of ".[" sequences inside ids.
  • Nodeids remain unchanged.
  • Add a regression test using pytester that asserts test_boo[a..[b]] appears unchanged in -vv headline output.

Notes

  • Yield tests have been removed since pytest 4.0; no remaining path should produce node segments like [0] that would justify the sanitization.
  • No CI run is necessary for this PR per project rules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions