pytest-dev/pytest · error · UsageError

Keyword expressions do not support call parameters.

Error message

Keyword expressions do not support call parameters.

What it means

Raised by `KeywordMatcher.__call__` when a `-k` keyword expression invokes a name with call parameters (parentheses with kwargs). pytest's `-k` expressions match substrings of test/keyword names only; they are not function calls, so `name(arg=value)` syntax is rejected with UsageError.

Source

Thrown at src/_pytest/mark/__init__.py:208

                continue
            mapped_names.add(node.name)

        # Add the names added as extra keywords to current or parent items.
        mapped_names.update(item.listextrakeywords())

        # Add the names attached to the current function through direct assignment.
        function_obj = getattr(item, "function", None)
        if function_obj:
            mapped_names.update(function_obj.__dict__)

        # Add the markers to the keywords as we no longer handle them correctly.
        mapped_names.update(mark.name for mark in item.iter_markers())

        return cls(mapped_names)

    def __call__(self, subname: str, /, **kwargs: str | int | bool | None) -> bool:
        if kwargs:
            raise UsageError("Keyword expressions do not support call parameters.")
        subname = subname.lower()
        return any(subname in name.lower() for name in self._names)


def deselect_by_keyword(items: list[Item], config: Config) -> None:
    keywordexpr = config.option.keyword.lstrip()
    if not keywordexpr:
        return

    expr = _parse_expression(keywordexpr, "Wrong expression passed to '-k'")

    remaining = []
    deselected = []
    for colitem in items:
        if not expr.evaluate(KeywordMatcher.from_item(colitem)):
            deselected.append(colitem)
        else:
            remaining.append(colitem)

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Use plain substring tokens in `-k`: `pytest -k "test_foo and login"`.
  2. To filter by parametrize ids, match the id substring: `pytest -k "user_admin"` (without parens).
  3. Use `-m` with markers for structured filtering instead of `-k`.

Example fix

// before
$ pytest -k "login(user=admin)"
// after
$ pytest -k "login and user_admin"
Defensive patterns

Strategy: validation

Validate before calling

def validate_keyword_expr(expr: str):
    import re
    if re.search(r"\w+\s*\([^)]*\)", expr):
        raise ValueError("Keyword expressions do not support call parameters")

Type guard

def is_plain_keyword_expr(expr: str) -> bool:
    import re
    return not re.search(r"\w+\s*\([^)]*\)", expr)

Prevention

When it happens

Trigger: Running `pytest -k "test_foo(bar)"` or `-k "login(user=admin)"`. The expression parser yields a matcher call with kwargs, which KeywordMatcher.__call__ rejects.

Common situations: Confusing `-k` keyword matching with marker/parametrize syntax. Trying to filter by parametrize ids via `-k` using call notation. Copying parametrize ids that contain parentheses into a `-k` expression.

Related errors


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