pytest-dev/pytest · error · UsageError

Too many fields ({len(parts)}), expected at most 5 separated

Error message

Too many fields ({len(parts)}), expected at most 5 separated by colons:

  action:message:category:module:line

For more information please consult: {doc_url}

What it means

Raised by parse_warning_filter() when a warning filter string has more than 5 colon-separated fields. Warning filters use the format action:message:category:module:line, which is exactly 5 fields max. The error is wrapped in a pytest UsageError with a helpful template and a link to the Python warnings documentation.

Source

Thrown at src/_pytest/config/__init__.py:2284

        {{error}}
        """
    )

    parts = arg.split(":")
    if len(parts) > 5:
        doc_url = (
            "https://docs.python.org/3/library/warnings.html#describing-warning-filters"
        )
        error = dedent(
            f"""\
            Too many fields ({len(parts)}), expected at most 5 separated by colons:

              action:message:category:module:line

            For more information please consult: {doc_url}
            """
        )
        raise UsageError(error_template.format(error=error))

    while len(parts) < 5:
        parts.append("")
    action_, message, category_, module, lineno_ = (s.strip() for s in parts)
    try:
        action: warnings._ActionKind = warnings._getaction(action_)  # type: ignore[attr-defined]
    except warnings._OptionError as e:
        raise UsageError(error_template.format(error=str(e))) from None
    try:
        category: type[Warning] = _resolve_warning_category(category_)
    except ImportError:
        raise
    except Exception:
        exc_info = ExceptionInfo.from_current()
        exception_text = exc_info.getrepr(style="native")
        raise UsageError(error_template.format(error=exception_text)) from None
    if message and escape:
        message = re.escape(message)

View on GitHub (pinned to 0d6fbdeffa)

Solutions

  1. Count colon-separated fields and reduce to at most 5: action:message:category:module:line
  2. If you need colons inside message or module regex, they cannot be used — restructure the filter
  3. Consult https://docs.python.org/3/library/warnings.html#describing-warning-filters for the format

Example fix

# pyproject.toml — before
[tool.pytest.ini_options]
filterwarnings = ["ignore::DeprecationWarning:mymodule:0:extra"]

# after
[tool.pytest.ini_options]
filterwarnings = ["ignore::DeprecationWarning:mymodule:0"]
Defensive patterns

Strategy: validation

Validate before calling

def validate_warning_filter(arg: str) -> bool:
    parts = arg.split(':')
    return len(parts) <= 5

Try / catch

from pytest import UsageError
try:
    parse_warning_filter(arg, escape=True)
except UsageError as e:
    print(f"Invalid warning filter: {e}")

Prevention

When it happens

Trigger: Providing a filterwarnings value or -W flag with more than 5 colon-delimited segments. Example: filterwarnings = ["ignore::DeprecationWarning:mymodule:0:extra"]. The `len(parts) > 5` check after arg.split(':') triggers.

Common situations: Misunderstanding the warning filter format and adding extra segments. Copying a malformed filter string from documentation or a Stack Overflow answer. Accidentally including a colon inside a module path or message regex (colons are field delimiters).

Related errors


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