{"id":"656b5b06d365f9c7","repo":"pytest-dev/pytest","slug":"directory-argument-cannot-contain-selection-par","errorCode":null,"errorMessage":"directory argument cannot contain :: selection parts: {arg}","messagePattern":"directory argument cannot contain :: selection parts: (.+?)","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/main.py","lineNumber":1180,"sourceCode":"        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))\n    if parts and fspath.is_dir():\n        msg = (\n            \"package argument cannot contain :: selection parts: {arg}\"\n            if as_pypath\n            else \"directory argument cannot contain :: selection parts: {arg}\"\n        )\n        raise UsageError(msg.format(arg=arg))\n    return CollectionArgument(\n        path=fspath,\n        parts=parts,\n        parametrization=parametrization,\n        module_name=module_name,\n        original_index=arg_index,\n    )\n\n\ndef is_collection_argument_subsumed_by(\n    arg: CollectionArgument, by: CollectionArgument\n) -> bool:\n    \"\"\"Check if `arg` is subsumed (contained) by `by`.\"\"\"\n    # First check path subsumption.\n    if by.path != arg.path:\n        # `by` subsumes `arg` if `by` is a parent directory of `arg` and has no\n        # parts (collects everything in that directory).\n        if not by.parts:","sourceCodeStart":1162,"sourceCodeEnd":1198,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/main.py#L1162-L1198","documentation":"Raised by `resolve_collection_argument` (filesystem path mode) when a directory argument also contains `::` selection parts. Selection parts (`::Class::test`) only apply to a Python module file, not to a directory, so pytest rejects selecting inside a bare directory.","triggerScenarios":"Running `pytest tests/::TestClass` or `pytest tests::test_foo`. The path is a directory and `parts` is non-empty.","commonSituations":"Assuming `::` works on directories to filter tests inside. Copying a node id that started from a file but trimmed the filename. Shell tab-completion quirks.","solutions":["Point at the specific file inside the directory: `pytest tests/test_foo.py::TestClass`.","Drop the `::` parts to collect the whole directory: `pytest tests/`.","Filter with `-k`/`-m` instead of node selection."],"exampleFix":"// before\n$ pytest tests/::TestClass\n// after\n$ pytest tests/test_foo.py::TestClass","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef validate_dir_no_selection(arg: str):\n    base = arg.split(\"::\", 1)[0]\n    if \"::\" in arg and Path(base).is_dir():\n        raise ValueError(f\"directory argument cannot contain '::': {arg}\")","typeGuard":"def selection_target_is_file(arg: str) -> bool:\n    from pathlib import Path\n    base = arg.split(\"::\", 1)[0]\n    p = Path(base)\n    return p.is_file() or not p.exists()","tryCatchPattern":null,"preventionTips":["Point '::' selection at a .py file, not a directory.","Drop '::' to collect an entire directory.","Use -k/-m for directory-level filtering."],"tags":["pytest","collection","cli","directory","usageerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}