pytest-dev/pytest · error · SyntaxError

escaping with "\" not supported in marker expression

Error message

escaping with "\" not supported in marker expression

What it means

The match-expression scanner explicitly forbids backslash escapes inside string literals. If a `\` appears anywhere between the opening and closing quote, this SyntaxError is raised at the backslash's column. Marker kwarg values are taken literally; no escape processing is performed.

Source

Thrown at src/_pytest/mark/expression.py:106

            elif input[pos] == ")":
                yield Token(TokenType.RPAREN, ")", pos)
                pos += 1
            elif input[pos] == "=":
                yield Token(TokenType.EQUAL, "=", pos)
                pos += 1
            elif input[pos] == ",":
                yield Token(TokenType.COMMA, ",", pos)
                pos += 1
            elif (quote_char := input[pos]) in ("'", '"'):
                end_quote_pos = input.find(quote_char, pos + 1)
                if end_quote_pos == -1:
                    raise SyntaxError(
                        f'closing quote "{quote_char}" is missing',
                        (FILE_NAME, 1, pos + 1, input),
                    )
                value = input[pos : end_quote_pos + 1]
                if (backslash_pos := value.find("\\")) != -1:
                    raise SyntaxError(
                        r'escaping with "\" not supported in marker expression',
                        (FILE_NAME, 1, pos + backslash_pos + 1, input),
                    )
                yield Token(TokenType.STRING, value, pos)
                pos += len(value)
            else:
                match = re.match(r"(:?\w|:|\+|-|\.|\[|\]|\\|/)+", input[pos:])
                if match:
                    value = match.group(0)
                    if value == "or":
                        yield Token(TokenType.OR, value, pos)
                    elif value == "and":
                        yield Token(TokenType.AND, value, pos)
                    elif value == "not":
                        yield Token(TokenType.NOT, value, pos)
                    else:
                        yield Token(TokenType.IDENT, value, pos)
                    pos += len(value)

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Remove backslashes from the kwarg value, or replace them with forward slashes / a different separator.
  2. If a literal backslash is genuinely required, move that logic out of the marker expression (use a real fixture/parametrize id instead).
  3. Use raw values that contain no `\`.

Example fix

# before
pytest -m 'filter(path="C:\\tmp")'
# after
pytest -m 'filter(path="C:/tmp")'
Defensive patterns

Strategy: validation

Validate before calling

def has_no_backslash_in_strings(expr: str) -> bool:
    # marker kwarg string values cannot contain a backslash
    return chr(92) not in expr

Prevention

When it happens

Trigger: Writing `-m 'foo(path="a\b")'` or otherwise trying to embed a backslash (Windows path, regex) inside a marker kwarg string.

Common situations: Pasting Windows file paths or regex patterns into a marker kwarg value; assuming Python string-escape semantics apply.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/f389a931ee3d64df.json. Report an issue: GitHub.