pytest-dev/pytest · error · SyntaxError
escaping with "\" not supported in marker expression
Error message
escaping with "\" not supported in marker expression
What it means
A SyntaxError raised by the marker-expression tokenizer when a quoted string value contains a backslash. pytest's expression grammar deliberately rejects escape sequences; the offending backslash position is reported via the SyntaxError offset tuple. It fires only after a successful close-quote match, while scanning the captured value.
Solutions
- Remove all backslashes from the marker value, or replace them with forward slashes / character classes.
- Pass complex data via a fixture or environment variable instead of the marker expression string.
- Define the marker with the value in code (not via -m) so escaping is handled by Python, not the expression grammar.
Example fix
# before pytest -m "my_mark(path='C:\\temp')" # after pytest -m "my_mark(path='C:/temp')"
Defensive patterns
Strategy: validation
Validate before calling
def is_safe_marker_value(value: str) -> bool:
return '\\' not in value Try / catch
try:
Expression.compile(expr)
except SyntaxError as e:
if 'escaping' in e.msg:
value = value.replace('\\', '/')
... Prevention
- Never use backslashes in marker expression values.
- Prefer forward slashes for paths.
- Pass rich data via fixtures, not marker strings.
When it happens
Trigger: Using `-m "my_mark(path='C:\\temp')"`, `-m "my_mark(regex='a\\b')"`, or any marker kwarg value containing a backslash. Even a single backslash before the closing quote triggers it because value.find('\\') is non-negative.
Common situations: Windows file paths or regex patterns passed as marker kwarg values through -m; copying a Python string literal (with escapes) into a CLI marker expression.
Related errors
- closing quote " " is missing
- unexpected character
- : : at column
- expected ; got
- not a valid python identifier
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/f389a931ee3d64df.
Report an issue: GitHub.
Appendix: 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 0d6fbdeffa)