pytest-dev/pytest · error · TypeError

{self.inipath}: config option '{name}' expects one of {_ini_

Error message

{self.inipath}: config option '{name}' expects one of {_ini_type_repr(type)}, got {builtins.type(value).__name__}: {value!r}

What it means

Raised by Config._getini (config/__init__.py:1843-1846) for a union (tuple) ini type when the supplied value could not be coerced/parsed by ANY member type. Each member is tried in turn inside _getini_value; if all raise TypeError/ValueError, this aggregate TypeError is raised and then wrapped as UsageError at line 1847-1848.

Source

Thrown at src/_pytest/config/__init__.py:1843

        mode = selected.mode

        # An invalid value is a user error, raised as UsageError so that it is
        # reported as a short message rather than an internal error traceback.
        try:
            if not isinstance(type, tuple):
                return self._getini_value(
                    mode, name, canonical_name, type, value, default
                )

            # Union: try each member; the first one that accepts the value wins.
            for member in type:
                try:
                    return self._getini_value(
                        mode, name, canonical_name, member, value, default
                    )
                except (TypeError, ValueError):
                    pass
            raise TypeError(
                f"{self.inipath}: config option '{name}' expects one of "
                f"{_ini_type_repr(type)}, got {builtins.type(value).__name__}: {value!r}"
            )
        except (TypeError, ValueError) as e:
            raise UsageError(str(e)) from e

    def _getini_value(
        self,
        mode: Literal["ini", "toml"],
        name: str,
        canonical_name: str,
        type: str | _IniLiteral,
        value: object,
        default: Any,
    ):
        """Convert a config value, read in the given mode, to the option's type."""
        if isinstance(type, _IniLiteral):
            # A Literal value is a plain string checked against the registered

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Read the error: it lists the expected types and the actual type/value — align the config value with one of them.
  2. If the value should be a string, quote it in TOML (my_opt = "value").
  3. If the value should be a bool, use true/false in TOML or yes/no in INI.
  4. Adjust the registered union in addini to include the type you actually need.

Example fix

# before (pyproject.toml)
[tool.pytest.ini_options]
addopts = 3   # option typed (bool|string)

# after
addopts = "-x"   # a string
Defensive patterns

Strategy: validation

Validate before calling

def check_union(value, types):
    accepted = False
    for t in types:
        try:
            if t == 'bool':
                accepted = isinstance(value, bool)
            elif t == 'string':
                accepted = isinstance(value, str)
            elif t == 'int':
                accepted = isinstance(value, int) and not isinstance(value, bool)
            if accepted:
                return
        except Exception:
            pass
    raise ValueError(f'value {value!r} matches none of {types}')

Type guard

def matches_union_member(value, member: str) -> bool:
    if member == 'bool': return isinstance(value, bool)
    if member == 'int':  return isinstance(value, int) and not isinstance(value, bool)
    if member == 'float': return isinstance(value, (int, float)) and not isinstance(value, bool)
    if member == 'string': return isinstance(value, str)
    return False

Try / catch

try:
    config.getini(name)
except Exception as e:  # UsageError wrapping TypeError
    # align the config value with one of the union members, then retry
    fix_and_reload(name)

Prevention

When it happens

Trigger: Registering addini with type=("bool","string") (or a Literal/union) and providing a value that matches none of the members — e.g. a TOML integer for an option typed as a string-or-bool union. Triggered on getini or any path that reads ini.

Common situations: Misconfigured pyproject.toml [tool.pytest.ini_options] supplying a native type incompatible with every member of a union option; combining string-only and bool-only types and feeding an int; migration from .ini (always str) to .toml exposing previously-hidden type mismatches.

Related errors


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