File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Fixed `| ` (pipe) not being treated as a regex meta-character that needs escaping in :func: `pytest.raises(match=...) <pytest.raises> `.
Original file line number Diff line number Diff line change @@ -363,14 +363,14 @@ def _check_raw_type(
363363
364364def is_fully_escaped (s : str ) -> bool :
365365 # we know we won't compile with re.VERBOSE, so whitespace doesn't need to be escaped
366- metacharacters = "{}()+.*?^$[]"
366+ metacharacters = "{}()+.*?^$[]| "
367367 return not any (
368368 c in metacharacters and (i == 0 or s [i - 1 ] != "\\ " ) for (i , c ) in enumerate (s )
369369 )
370370
371371
372372def unescape (s : str ) -> str :
373- return re .sub (r"\\([{}()+-.*?^$\[\]\s\\])" , r"\1" , s )
373+ return re .sub (r"\\([{}()+-.*?^$\[\]\s\\| ])" , r"\1" , s )
374374
375375
376376# These classes conceptually differ from ExceptionInfo in that ExceptionInfo is tied, and
Original file line number Diff line number Diff line change @@ -430,3 +430,12 @@ def test_raises_match_compiled_regex(self) -> None:
430430 pattern_with_flags = re .compile (r"INVALID LITERAL" , re .IGNORECASE )
431431 with pytest .raises (ValueError , match = pattern_with_flags ):
432432 int ("asdf" )
433+
434+ def test_pipe_is_treated_as_regex_metacharacter (self ) -> None :
435+ """| (pipe) must be recognized as a regex metacharacter."""
436+ from _pytest .raises import is_fully_escaped
437+ from _pytest .raises import unescape
438+
439+ assert not is_fully_escaped ("foo|bar" )
440+ assert is_fully_escaped (r"foo\|bar" )
441+ assert unescape (r"foo\|bar" ) == "foo|bar"
You can’t perform that action at this time.
0 commit comments