pytest-dev/pytest · error · UsageError

path cannot contain [] parametrization: {arg}

Error message

path cannot contain [] parametrization: {arg}

What it means

Raised by `resolve_collection_argument` when a command-line path argument contains a `[...]` parametrization bracket but no `::` selection part before it. Parametrization IDs may only be appended to a concrete test node selection (`file::node`), never to a bare path.

Source

Thrown at src/_pytest/main.py:1155

        "pkg.tests.test_foo::TestClass::test_foo[a,b]"

    In which case we search sys.path for a matching module, and then return the *path* to the
    found module, which may look like this:

        CollectionArgument(
            path=Path("/home/u/myvenv/lib/site-packages/pkg/tests/test_foo.py"),
            parts=["TestClass", "test_foo"],
            parametrization="[a,b]",
            module_name="pkg.tests.test_foo",
        )

    If the path doesn't exist, raise UsageError.
    If the path is a directory and selection parts are present, raise UsageError.
    """
    base, squacket, rest = arg.partition("[")
    strpath, *parts = base.split("::")
    if squacket and not parts:
        raise UsageError(f"path cannot contain [] parametrization: {arg}")
    parametrization = f"{squacket}{rest}" if squacket else None
    module_name = None
    if as_pypath:
        pyarg_strpath = search_pypath(
            strpath, consider_namespace_packages=consider_namespace_packages
        )
        if pyarg_strpath is not None:
            module_name = strpath
            strpath = pyarg_strpath
    fspath = invocation_path / strpath
    fspath = absolutepath(fspath)
    if not safe_exists(fspath):
        msg = (
            "module or package not found: {arg} (missing __init__.py?)"
            if as_pypath
            else "file or directory not found: {arg}"
        )
        raise UsageError(msg.format(arg=arg))

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Add the `::` selection before the brackets: `pytest "tests/test_foo.py::test_case[a-b]"`.
  2. To run all cases of a test, omit the brackets: `pytest tests/test_foo.py::test_case`.
  3. Quote the argument in the shell to avoid glob expansion of `[`.

Example fix

// before
$ pytest "tests/test_foo.py[a-b]"
// after
$ pytest "tests/test_foo.py::test_case[a-b]"
Defensive patterns

Strategy: validation

Validate before calling

def validate_node_id(arg: str):
    base, bracket, _ = arg.partition("[")
    parts = base.split("::")
    if bracket and len(parts) == 1:
        raise ValueError(f"path cannot contain [] parametrization without '::': {arg}")
    return arg

Type guard

def node_id_has_selection(arg: str) -> bool:
    return "::" in arg.partition("[")[0]

Prevention

When it happens

Trigger: Running `pytest "tests/test_foo.py[a-b]"` (parametrization with no `::` test id), or `pytest "tests[a]"`. The partition finds a `[` but `parts` (from `::` split) is empty.

Common situations: Misremembering the selection syntax. Trying to run all parametrized cases of a file at once via brackets. Shell auto-completion producing an incomplete node id.

Related errors


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