Skip to content

raises: add missing | metacharacter to is_fully_escaped and unescape#14222

Merged
bluetech merged 1 commit intopytest-dev:mainfrom
bysiber:fix-missing-pipe-metacharacter
Mar 20, 2026
Merged

raises: add missing | metacharacter to is_fully_escaped and unescape#14222
bluetech merged 1 commit intopytest-dev:mainfrom
bysiber:fix-missing-pipe-metacharacter

Conversation

@bysiber
Copy link
Copy Markdown
Contributor

@bysiber bysiber commented Feb 20, 2026

is_fully_escaped checks whether a match pattern body (inside ^...$) contains only escaped regex metacharacters, so pytest knows it can treat it as a literal string for diff output. The current metacharacter list is:

metacharacters = "{}()+.*?^$[]"

This is missing |, the regex alternation operator. A pattern like ^foo|bar$ would pass the is_fully_escaped check even though the | makes it a regex alternation rather than a literal. This causes rawmatch to be set to the unescaped value foo|bar, leading to a misleading diff in the failure message.

The unescape function has the same omission in its character class, so \| wouldn't be unescaped even if someone properly escaped it.

This adds | to both the metacharacter list in is_fully_escaped and the character class in unescape.

@The-Compiler
Copy link
Copy Markdown
Member

Similarly to your other PRs I commented on, could you please add a regression test to catch this issue?

@bysiber
Copy link
Copy Markdown
Contributor Author

bysiber commented Feb 20, 2026

Added a regression test that verifies | is recognized as a regex metacharacter in is_fully_escaped, and that unescape properly handles escaped pipes.

The pipe character is a regex metacharacter (alternation operator) but
is_fully_escaped did not include it, so a match pattern like '^foo|bar$'
would be wrongly identified as a fully-escaped literal. This causes
rawmatch to be set incorrectly, leading to misleading diff output on
match failure.

Also add | to the unescape function's character class for consistency.
@bluetech bluetech force-pushed the fix-missing-pipe-metacharacter branch from 67f1959 to 1613a12 Compare March 20, 2026 11:34
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Mar 20, 2026
Copy link
Copy Markdown
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

I tweaked the test a bit and added a changelog.

@bluetech bluetech added the backport 9.0.x apply to PRs at any point; backports the changes to the 9.0.x branch label Mar 20, 2026
@bluetech bluetech enabled auto-merge March 20, 2026 11:36
@bluetech bluetech merged commit 960ddc6 into pytest-dev:main Mar 20, 2026
33 checks passed
@patchback
Copy link
Copy Markdown

patchback Bot commented Mar 20, 2026

Backport to 9.0.x: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 960ddc6 on top of patchback/backports/9.0.x/960ddc6f67d7a37b8390f260f8ded2049f8a28de/pr-14222

Backporting merged PR #14222 into main

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/pytest-dev/pytest.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/9.0.x/960ddc6f67d7a37b8390f260f8ded2049f8a28de/pr-14222 upstream/9.0.x
  4. Now, cherry-pick PR raises: add missing | metacharacter to is_fully_escaped and unescape #14222 contents into that branch:
    $ git cherry-pick -x 960ddc6f67d7a37b8390f260f8ded2049f8a28de
    If it'll yell at you with something like fatal: Commit 960ddc6f67d7a37b8390f260f8ded2049f8a28de is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 960ddc6f67d7a37b8390f260f8ded2049f8a28de
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR raises: add missing | metacharacter to is_fully_escaped and unescape #14222 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/9.0.x/960ddc6f67d7a37b8390f260f8ded2049f8a28de/pr-14222
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 9.0.x apply to PRs at any point; backports the changes to the 9.0.x branch bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants