pytest-dev/pytest · error · TypeError
{self.inipath}: config option '{name}' expects a list of str
Error message
{self.inipath}: config option '{name}' expects a list of strings, but item at index {i} is {item_type}: {item!r} What it means
Raised by Config._getini_toml (config/__init__.py:1965-1971) for a 'paths'-typed option in TOML mode when the value IS a list but at least one element is not a str. The message names the offending index, item type, and value to pinpoint the bad entry.
Source
Thrown at src/_pytest/config/__init__.py:1968
default: Any,
):
"""Handle TOML config values with strict type validation and no coercion.
In TOML mode, values already have native types from TOML parsing.
We validate types match expectations exactly, including list items.
"""
value_type = builtins.type(value).__name__
if type == "paths":
# Expect a list of strings.
if not isinstance(value, list):
raise TypeError(
f"{self.inipath}: config option '{name}' expects a list for type 'paths', "
f"got {value_type}: {value!r}"
)
for i, item in enumerate(value):
if not isinstance(item, str):
item_type = builtins.type(item).__name__
raise TypeError(
f"{self.inipath}: config option '{name}' expects a list of strings, "
f"but item at index {i} is {item_type}: {item!r}"
)
dp = (
self.inipath.parent
if self.inipath is not None
else self.invocation_params.dir
)
return [dp / x for x in value]
elif type in {"args", "linelist"}:
# Expect a list of strings.
if not isinstance(value, list):
raise TypeError(
f"{self.inipath}: config option '{name}' expects a list for type '{type}', "
f"got {value_type}: {value!r}"
)
for i, item in enumerate(value):
if not isinstance(item, str):View on GitHub (pinned to 98b357f69e)
Solutions
- Ensure every element of the paths array is a quoted TOML string.
- Use the reported index to find and fix the specific bad entry.
- Remove any non-path elements (numbers/bools) from the array.
Example fix
# before (pyproject.toml) testpaths = ["tests", 42] # after testpaths = ["tests", "tests_42"]
Defensive patterns
Strategy: validation
Validate before calling
def validate_paths_items(value):
for i, item in enumerate(value):
if not isinstance(item, str):
raise TypeError(f'paths[{i}] must be str, got {type(item).__name__}')
return value Type guard
def all_items_str(value) -> bool:
return isinstance(value, list) and all(isinstance(x, str) for x in value) Try / catch
try:
config.getini(name)
except Exception:
# stringify/remove non-path entries and reload Prevention
- Quote every entry of TOML path arrays.
- Avoid mixing types in a single array.
- Use the reported index to locate the bad item quickly.
When it happens
Trigger: Supplying a TOML array for testpaths/pythonpath/norecursedirs where one element is an int, bool, or nested table, e.g. testpaths = ["tests", 42].
Common situations: Mixed-type arrays after a partial TOML edit; copy-paste from YAML/JSON leaving unquoted numerics; booleans (true/false) accidentally included as path entries.
Related errors
- {self.inipath}: config option '{name}' expects a list for ty
- {self.inipath}: config option '{name}' expects one of {_ini_
- {self.inipath}: config option '{name}' expects a string, got
- {self.inipath}: config option '{name}' expects a list for ty
- {self.inipath}: config option '{name}' expects a bool, got {
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/f5016b8dd772f962.json.
Report an issue: GitHub.