pytest-dev/pytest · error · SyntaxError

closing quote "{quote_char}" is missing

Error message

closing quote "{quote_char}" is missing

What it means

Raised by the match-expression scanner when a string literal in marker kwargs starts with a quote but never closes, e.g. `foo(x="bar)`. The scanner looks for the matching quote and fails, producing a SyntaxError with the column offset of the opening quote. The expression grammar only allows string values inside `ident(name=value)` kwargs.

Source

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

        while pos < len(input):
            if input[pos] in (" ", "\t"):
                pos += 1
            elif input[pos] == "(":
                yield Token(TokenType.LPAREN, "(", pos)
                pos += 1
            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":

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Match opening and closing quote characters exactly inside kwarg string values.
  2. Single-quote the whole `-m` argument on the shell and use double quotes inside, or vice versa.
  3. Validate the expression in a Python REPL via `_pytest.mark.expression.Expression.compile(...)` before committing it to CI.

Example fix

# before
pytest -m 'slow(reason="too slow)'
# after
pytest -m 'slow(reason="too slow")'
Defensive patterns

Strategy: validation

Validate before calling

def balanced_quotes(expr: str) -> bool:
    return expr.count(chr(39)) % 2 == 0 and expr.count(chr(34)) % 2 == 0

Try / catch

try:
    Expression.compile(expr)
except SyntaxError as e:
    if 'closing quote' in e.msg:
        ...

Prevention

When it happens

Trigger: Using `-m 'foo(reason="missing)'` or a parametrized-style marker call where the closing quote is omitted or mismatched (single vs double).

Common situations: Shell quoting eats the closing quote; copy-pasting a marker expression that was split across lines; mismatching `"` and `'` in kwargs.

Related errors


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