{"record":{"id":"b52bcb38cbf21b88","repo":"pytest-dev/pytest","slug":"path-cannot-contain-parametrization-arg","errorCode":null,"errorMessage":"path cannot contain [] parametrization: {arg}","messagePattern":"path cannot contain \\[\\] parametrization: (.+?)","errorType":"validation","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/main.py","lineNumber":1155,"sourceCode":"        \"pkg.tests.test_foo::TestClass::test_foo[a,b]\"\n\n    In which case we search sys.path for a matching module, and then return the *path* to the\n    found module, which may look like this:\n\n        CollectionArgument(\n            path=Path(\"/home/u/myvenv/lib/site-packages/pkg/tests/test_foo.py\"),\n            parts=[\"TestClass\", \"test_foo\"],\n            parametrization=\"[a,b]\",\n            module_name=\"pkg.tests.test_foo\",\n        )\n\n    If the path doesn't exist, raise UsageError.\n    If the path is a directory and selection parts are present, raise UsageError.\n    \"\"\"\n    base, squacket, rest = arg.partition(\"[\")\n    strpath, *parts = base.split(\"::\")\n    if squacket and not parts:\n        raise UsageError(f\"path cannot contain [] parametrization: {arg}\")\n    parametrization = f\"{squacket}{rest}\" if squacket else None\n    module_name = None\n    if as_pypath:\n        pyarg_strpath = search_pypath(\n            strpath, consider_namespace_packages=consider_namespace_packages\n        )\n        if pyarg_strpath is not None:\n            module_name = strpath\n            strpath = pyarg_strpath\n    fspath = invocation_path / strpath\n    fspath = absolutepath(fspath)\n    if not safe_exists(fspath):\n        msg = (\n            \"module or package not found: {arg} (missing __init__.py?)\"\n            if as_pypath\n            else \"file or directory not found: {arg}\"\n        )\n        raise UsageError(msg.format(arg=arg))","sourceCodeStart":1137,"sourceCodeEnd":1173,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/main.py#L1137-L1173","documentation":"resolve_collection_argument (main.py:1155) splits the argument on '[' to detect parametrization like 'test_file.py::test_func[a-b]'. If a '[' appears but there are no '::' selection parts beforehand, the brackets cannot attach to any test ID, so pytest raises UsageError('path cannot contain [] parametrization: {arg}'). The brackets are only valid as a suffix to a '::' selection.","triggerScenarios":"Running 'pytest tests[a-b].py' (brackets on the file path); 'pytest path[1]' with no '::'; misusing glob/shell expansion that injects brackets into a path argument.","commonSituations":"Shell glob producing bracketed paths; typos placing parametrization before '::'; attempting to parametrize a file path rather than a test.","solutions":["Move the bracket suffix to after a '::' selection: 'path::test_name[param]'.","Quote the path argument to prevent shell glob expansion injecting brackets.","Rename files/dirs that literally contain '[' or ']' characters."],"exampleFix":"// before\npytest tests[smoke].py\n// after\npytest tests/test_smoke.py::test_login[guest]","handlingStrategy":"validation","validationCode":"import re\ndef validate_arg(arg: str) -> str:\n    if '[' in arg and '::' not in arg.split('[')[0]:\n        raise ValueError(f'brackets require a :: selection: {arg!r}')\n    return arg","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Quote path arguments to avoid shell glob bracket injection.","Place parametrization brackets only after '::'.","Avoid '[' / ']' in real file/directory names."],"tags":["pytest","collection","cli","parametrization","argument"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}