pytest-dev/pytest · error · TypeError

Expected an int string for option {name} of type integer, bu

Error message

Expected an int string for option {name} of type integer, but got: {value!r}

What it means

Raised by Config._getini_ini (config/__init__.py:1929-1933) for an option of type 'int' when, in INI mode, the value is not a str. INI files always yield str/list[str], so a non-str here indicates the value arrived from a non-ini source masquerading as ini mode (e.g. a programmatic override injecting an int). int(value) is only attempted on str.

Source

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

                if self.inipath is not None
                else self.invocation_params.dir
            )
            input_values = shlex.split(value) if isinstance(value, str) else value
            return [dp / x for x in input_values]
        elif type == "args":
            return shlex.split(value) if isinstance(value, str) else value
        elif type == "linelist":
            if isinstance(value, str):
                return [t for t in map(lambda x: x.strip(), value.split("\n")) if t]
            else:
                return value
        elif type == "bool":
            return _strtobool(str(value).strip())
        elif type == "string":
            return value
        elif type == "int":
            if not isinstance(value, str):
                raise TypeError(
                    f"Expected an int string for option {name} of type integer, but got: {value!r}"
                ) from None
            return int(value)
        elif type == "float":
            if not isinstance(value, str):
                raise TypeError(
                    f"Expected a float string for option {name} of type float, but got: {value!r}"
                ) from None
            return float(value)
        else:
            return self._getini_unknown_type(name, type, value)

    def _getini_toml(
        self,
        name: str,
        canonical_name: str,
        type: str,
        value: object,

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Ensure int-typed ini values pass through the parser as strings (the normal .ini path) so int(value) can coerce them.
  2. If injecting programmatically, str() the value before placing it into _inicfg, or use getini via the proper API.
  3. Prefer setting int options through CLI flags or pyproject.toml rather than direct dict mutation.

Example fix

# before
config._inicfg['count'] = ConfigValue(value=5, ...)  # int, not str

# after
config._inicfg['count'] = ConfigValue(value='5', ...)
Defensive patterns

Strategy: validation

Validate before calling

def coerce_int_str(value):
    if not isinstance(value, str):
        value = str(value)
    int(value)  # validate parseable
    return value

Type guard

def is_int_str(value) -> bool:
    return isinstance(value, str) and value.lstrip('-').isdigit()

Try / catch

try:
    config.getini(name)
except (TypeError, ValueError):
    # normalize injected value to str and reload

Prevention

When it happens

Trigger: Calling _getini_ini directly with a non-str value for an int-typed option, or a conftest/CLI override that injects an int into _inicfg under ini mode for an int option. Not reachable from a well-formed .ini file read.

Common situations: Programmatic test configuration that mutates config._inicfg with native ints; plugin internals bypassing the normal parse path; rare edge cases after partial refactors of the config layer.

Related errors


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