A compiled match expression as used by -k and -m. The expression can be evaluated against different matchers.
| 309 | |
| 310 | @final |
| 311 | class Expression: |
| 312 | """A compiled match expression as used by -k and -m. |
| 313 | |
| 314 | The expression can be evaluated against different matchers. |
| 315 | """ |
| 316 | |
| 317 | __slots__ = ("_code", "input") |
| 318 | |
| 319 | def __init__(self, input: str, code: types.CodeType) -> None: |
| 320 | #: The original input line, as a string. |
| 321 | self.input: Final = input |
| 322 | self._code: Final = code |
| 323 | |
| 324 | @classmethod |
| 325 | def compile(cls, input: str) -> Expression: |
| 326 | """Compile a match expression. |
| 327 | |
| 328 | :param input: The input expression - one line. |
| 329 | |
| 330 | :raises SyntaxError: If the expression is malformed. |
| 331 | """ |
| 332 | astexpr = expression(Scanner(input)) |
| 333 | code = compile( |
| 334 | astexpr, |
| 335 | filename="<pytest match expression>", |
| 336 | mode="eval", |
| 337 | ) |
| 338 | return Expression(input, code) |
| 339 | |
| 340 | def evaluate(self, matcher: ExpressionMatcher) -> bool: |
| 341 | """Evaluate the match expression. |
| 342 | |
| 343 | :param matcher: |
| 344 | A callback which determines whether an identifier matches or not. |
| 345 | See the :class:`ExpressionMatcher` protocol for details and example. |
| 346 | |
| 347 | :returns: Whether the expression matches or not. |
| 348 | |
| 349 | :raises UsageError: |
| 350 | If the matcher doesn't support the expression. Cannot happen if the |
| 351 | matcher supports all expressions. |
| 352 | """ |
| 353 | return bool(eval(self._code, {"__builtins__": {}}, MatcherAdapter(matcher))) |