larksuite/cli · error · ValueError

expected integer

Error message

expected integer

What it means

builtin_scalar_value validates integer-family XSD types by requiring the lexical value to match [+-]?\d+ before converting with Decimal. If the string is not a well-formed integer (e.g. '1.5', '1e3', ' 1', 'abc') it raises ValueError('expected integer'). Note the related ArithmeticError('expected positive integer')/'expected non-negative integer' fires only when the digits parse but violate the type's range.

Source

Thrown at skills/lark-slides/scripts/sxsd_validator.py:493

        "actual": actual,
        "message": message,
        "hint": hint,
    }
    if attr is not None:
        result["attr"] = attr
    return result


def builtin_scalar_value(type_name: str, value: str) -> Decimal | str | bool:
    if type_name in {"string", "anyURI"}:
        return value
    if type_name == "boolean":
        if value not in {"true", "false", "1", "0"}:
            raise ValueError("expected boolean")
        return value in {"true", "1"}
    if type_name in {"integer", "positiveInteger", "nonNegativeInteger"}:
        if re.fullmatch(r"[+-]?\d+", value) is None:
            raise ValueError("expected integer")
        number = Decimal(value)
        if type_name == "positiveInteger" and number <= 0:
            raise ArithmeticError("expected positive integer")
        if type_name == "nonNegativeInteger" and number < 0:
            raise ArithmeticError("expected non-negative integer")
        return number
    if type_name in {"double", "decimal"}:
        lexical_value = value.strip(" \t\n\r")
        decimal_pattern = r"[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)"
        double_pattern = decimal_pattern + r"(?:[eE][+-]?[0-9]+)?"
        expected_pattern = double_pattern if type_name == "double" else decimal_pattern
        if re.fullmatch(expected_pattern, lexical_value) is None:
            raise ValueError(f"expected {type_name}")
        try:
            number = Decimal(lexical_value)
        except InvalidOperation as error:
            raise ValueError(f"expected {type_name}") from error
        if not math.isfinite(float(number)):

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Normalize the value to a plain base-10 integer string before validation: str(int(float(value))) for floats, or strip separators/formatting first.
  2. Ensure float data is serialized without a decimal point when the schema type is integer (e.g. int(value) before writing).
  3. If the value genuinely has a fractional part, change the schema type to decimal/double instead of coercing the data.
  4. Pre-check with re.fullmatch(r'[+-]?\d+', value) in the caller for a clearer domain-specific error message.

Example fix

// before
validate("count", "1e3", type="integer")  # ValueError: expected integer

// after
value = str(int(float(raw)))          # '1e3' -> '1000'
validate("count", value, type="integer")
Defensive patterns

Strategy: validation

Validate before calling

import re

def is_xsd_integer(value: str) -> bool:
    return re.fullmatch(r"[+-]?\d+", value) is not None

if not is_xsd_integer(raw):
    raise SystemExit(f"{raw!r} is not an XSD integer literal (no decimals, exponents, or separators)")

Type guard

import re

def is_xsd_integer(value: str) -> bool:
    return re.fullmatch(r"[+-]?\d+", value) is not None

Try / catch

try:
    parsed = builtin_scalar_value("integer", raw)
except ValueError as err:
    if str(err) == "expected integer":
        try:
            raw = str(int(float(raw)))
        except (TypeError, ValueError):
            raise SystemExit(f"not an integer: {raw!r}") from None
        parsed = builtin_scalar_value("integer", raw)
    else:
        raise

Prevention

When it happens

Trigger: Validating an element/attribute typed as integer, positiveInteger, or nonNegativeInteger whose lexical value contains a decimal point, exponent, whitespace, thousands separators, or non-digits — anything failing re.fullmatch(r'[+-]?\d+', value).

Common situations: Feeding JSON/CSV numeric data straight into the validator where floats like 3.0 or exponent notation like 1e3 are common; locale-formatted numbers ('1,000'); values pulled from free-text input; scientific-notation serializers.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/b92d6e9ad6b594eb. Report an issue: GitHub.