pytest-dev/pytest · error · SyntaxError

closing quote " " is missing

Error message

closing quote "{quote_char}" is missing

What it means

A SyntaxError raised by the marker-expression tokenizer when it finds an opening single or double quote with no matching close quote before end of input. It carries the FILE_NAME, line 1, the 1-based column of the opening quote, and the raw input so pytest can render a caret. String literals are used for kwarg values inside marker expressions like `@pytest.mark.foo(bar="x")` parsed via -m.

Solutions

  1. Inspect the column reported in the error; it points at the unmatched opening quote.
  2. Add the matching close quote of the same character.
  3. Quote the entire -m argument with single quotes in the shell and use double quotes inside, or vice versa.
  4. Avoid backslashes in marker values (see error 122); they are unsupported.

Example fix

# before
pytest -m "my_mark(name='value)"
# after
pytest -m "my_mark(name='value')"
Defensive patterns

Strategy: validation

Validate before calling

def has_balanced_quotes(s: str) -> bool:
    for q in ("'", '"'):
        if s.count(q) % 2 != 0:
            return False
    return True

Try / catch

try:
    Expression.compile(expr)
except SyntaxError as e:
    if 'closing quote' in e.msg:
        # prompt user to fix quoting
        ...

Prevention

When it happens

Trigger: Passing `-m "my_mark(name='value)"` (missing close quote), `-m "my_mark(\"x)"`, or any marker expression whose quoted value is truncated. The tokenizer enters the quote branch because input[pos] is ' or \" and input.find never returns a second quote.

Common situations: Shell escaping mistakes (the shell consumes the closing quote), generating marker expressions from untrusted strings without escaping, or hand-editing a marker filter and dropping the trailing quote.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/bfafaf863c17acf9. Report an issue: GitHub.

Appendix: 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 0d6fbdeffa)