{"id":"776d17a9d61aab93","repo":"pytest-dev/pytest","slug":"args-parameter-expected-to-be-a-list-of-strings","errorCode":null,"errorMessage":"`args` parameter expected to be a list of strings, got: {!r} (type: {})","messagePattern":"`args` parameter expected to be a list of strings, got: (.+?) \\(type: (.+?)\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":406,"sourceCode":"    This function can be used by integration with other tools, like hooking\n    into pytest to run tests into an IDE.\n    \"\"\"\n    return get_config().pluginmanager\n\n\ndef _prepareconfig(\n    args: list[str] | os.PathLike[str],\n    plugins: Sequence[str | _PluggyPlugin] | None = None,\n    *,\n    prog: str | None = None,\n) -> Config:\n    if isinstance(args, os.PathLike):\n        args = [os.fspath(args)]\n    elif not isinstance(args, list):\n        msg = (  # type:ignore[unreachable]\n            \"`args` parameter expected to be a list of strings, got: {!r} (type: {})\"\n        )\n        raise TypeError(msg.format(args, type(args)))\n\n    initial_config = get_config(args, plugins, prog=prog)\n    pluginmanager = initial_config.pluginmanager\n    try:\n        if plugins:\n            for plugin in plugins:\n                if isinstance(plugin, str):\n                    pluginmanager.consider_pluginarg(plugin)\n                else:\n                    pluginmanager.register(plugin)\n        config: Config = pluginmanager.hook.pytest_cmdline_parse(\n            pluginmanager=pluginmanager, args=args\n        )\n        return config\n    except BaseException:\n        initial_config._ensure_unconfigure()\n        raise\n","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/config/__init__.py#L388-L424","documentation":"The _prepareconfig function requires the args parameter to be a list of strings (or a single os.PathLike). If args is any other type (tuple, string, dict, int, etc.), pytest raises TypeError. This is a guard for programmatic pytest.main() callers who pass the wrong collection type.","triggerScenarios":"Calling pytest.main(args) or pytest.main(args, plugins) where args is a string (e.g., 'tests/'), a tuple, or another non-list type. The isinstance(args, list) check fails and TypeError is raised.","commonSituations":"Developers calling pytest.main() programmatically and passing a single string like '-v -s tests/' instead of ['-v', '-s', 'tests/']. Also migration from older pytest versions where looser typing was tolerated.","solutions":["Pass args as a list of strings: pytest.main(['-v', 'tests/']).","If args is a tuple, convert it: pytest.main(list(args)).","If args is a single PathLike, it is accepted directly; otherwise wrap any string in a list."],"exampleFix":"# before\nimport pytest\npytest.main('-v -s tests/')  # string, not list\n\n# after\nimport pytest\npytest.main(['-v', '-s', 'tests/'])","handlingStrategy":"type-guard","validationCode":"import os\nfrom collections.abc import Sequence\n\ndef coerce_pytest_args(args):\n    if isinstance(args, os.PathLike):\n        return [os.fspath(args)]\n    if isinstance(args, list):\n        return args\n    if isinstance(args, str):\n        # A single string is ambiguous; split on spaces if it looks like CLI flags\n        return args.split()\n    if isinstance(args, Sequence):\n        return list(args)\n    raise TypeError(f\"args must be a list of strings, got {type(args).__name__}\")\n\n# usage: pytest.main(coerce_pytest_args(my_args))","typeGuard":"from collections.abc import Sequence\nimport os\n\ndef is_valid_pytest_args(args) -> bool:\n    return isinstance(args, (list, os.PathLike)) or (isinstance(args, str) and False)  # str alone is NOT valid","tryCatchPattern":null,"preventionTips":["Always construct pytest.main args as a list of strings from the start.","Add a type annotation args: list[str] to catch issues with static type checkers.","Never pass a raw string of flags; always split into a list."],"tags":["api","type-error","programmatic-invocation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}