MCPcopy
hub / github.com/pytest-dev/pytest / __init__

Method __init__

src/_pytest/raises.py:367–417  ·  view source on GitHub ↗
(
        self,
        *,
        match: str | Pattern[str] | None,
        check: Callable[[BaseExcT_co], bool] | None,
    )

Source from the content-addressed store, hash-verified

365 """ABC with common functionality shared between RaisesExc and RaisesGroup"""
366
367 def __init__(
368 self,
369 *,
370 match: str | Pattern[str] | None,
371 check: Callable[[BaseExcT_co], bool] | None,
372 ) -> None:
373 if isinstance(match, str):
374 # juggle error in order to avoid context to fail (necessary?)
375 re_error = None
376 try:
377 self.match: Pattern[str] | None = re.compile(match)
378 except re.error as e:
379 re_error = e
380 if re_error is not None:
381 fail(f"Invalid regex pattern provided to 'match': {re_error}")
382 if match == "":
383 warnings.warn(
384 PytestWarning(
385 "matching against an empty string will *always* pass. If you want "
386 "to check for an empty message you need to pass '^$'. If you don't "
387 "want to match you should pass `None` or leave out the parameter."
388 ),
389 stacklevel=2,
390 )
391 else:
392 self.match = match
393
394 # check if this is a fully escaped regex and has ^$ to match fully
395 # in which case we can do a proper diff on error
396 self.rawmatch: str | None = None
397 if isinstance(match, str) or (
398 isinstance(match, Pattern) and match.flags == _REGEX_NO_FLAGS
399 ):
400 if isinstance(match, Pattern):
401 match = match.pattern
402 if (
403 match
404 and match[0] == "^"
405 and match[-1] == "$"
406 and is_fully_escaped(match[1:-1])
407 ):
408 self.rawmatch = unescape(match[1:-1])
409
410 self.check = check
411 self._fail_reason: str | None = None
412
413 # used to suppress repeated printing of `repr(self.check)`
414 self._nested: bool = False
415
416 # set in self._parse_exc
417 self.is_baseexception = False
418
419 def _parse_exc(
420 self, exc: type[BaseExcT_1] | types.GenericAlias, expected: str

Callers

nothing calls this directly

Calls 5

PytestWarningClass · 0.90
is_fully_escapedFunction · 0.85
unescapeFunction · 0.85
compileMethod · 0.80
warnMethod · 0.45

Tested by

no test coverage detected