{"record":{"id":"06c4a727f9292b27","repo":"bazelbuild/bazel","slug":"got-s-want-a-finite-number","errorCode":null,"errorMessage":"got %s, want a finite number","messagePattern":"got (.+?), want a finite number","errorType":"exception","errorClass":"MissingFormatWidthException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/eval/Printer.java","lineNumber":433,"sourceCode":"            \"not enough arguments for format pattern \"\n                + Starlark.repr(pattern, semantics)\n                + \": \"\n                + Starlark.repr(Tuple.copyOf(arguments), semantics));\n      }\n      Object arg = arguments.get(a++);\n\n      switch (conv) {\n        case 'd', 'o', 'x', 'X' -> {\n          Number n =\n              switch (arg) {\n                case StarlarkInt starlarkInt -> starlarkInt.toNumber();\n                case Integer integer -> integer;\n                case StarlarkFloat starlarkFloat -> {\n                  double d = starlarkFloat.toDouble();\n                  try {\n                    yield StarlarkInt.ofFiniteDouble(d).toNumber();\n                  } catch (IllegalArgumentException unused) {\n                    throw new MissingFormatWidthException(\"got \" + arg + \", want a finite number\");\n                  }\n                }\n                default ->\n                    throw new MissingFormatWidthException(\n                        String.format(\n                            \"got %s for '%%%c' format, want int or float\",\n                            Starlark.type(arg), conv));\n              };\n          printer.append(\n              String.format(\n                  conv == 'd' ? \"%d\" : conv == 'o' ? \"%o\" : conv == 'x' ? \"%x\" : \"%X\", n));\n        }\n\n        case 'e', 'f', 'g', 'E', 'F', 'G' -> {\n          double v =\n              switch (arg) {\n                case Integer integer -> (double) integer;\n                case StarlarkInt starlarkInt -> starlarkInt.toDouble();","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/eval/Printer.java#L415-L451","documentation":"Thrown by %d/%o/%x/%X formatting when the argument is a StarlarkFloat that is infinite or NaN. The printer tries StarlarkInt.ofFiniteDouble to coerce the float to an integer; ofFiniteDouble rejects non-finite/non-integral-representable doubles with IllegalArgumentException, which is converted to this MissingFormatWidthException.","triggerScenarios":"\"%d\" % float(\"inf\"), \"%x\" % float(\"nan\"), \"%d\" % (1e300 / 1e300 * float('nan')), or any arithmetic that yields inf/nan (division overflows, float(\"1e400\")) followed by %d formatting.","commonSituations":"Formatting computed ratios or scores that can divide by zero; parsing user input like 'inf'/'nan' with float(); JSON-decoded floats carrying NaN before %d logging.","solutions":["Use a float conversion (%f/%g/%e) instead of %d for non-integral or possibly non-finite values.","Sanitize before formatting: replace non-finite values (v == v and abs(v) != float(\"inf\")) with 0 or a sentinel.","Fix the upstream computation that produced inf/nan (guard divisors, check for zero denominators)."],"exampleFix":"# before\n\"%d items\" % (total / count)  # count can be 0 -> inf/nan\n\n# after\n\"%g items\" % (total / count) if count else \"n/a\"","handlingStrategy":"validation","validationCode":"def finite_int(v):\n    if type(v) == \"float\" and (v != v or v == float(\"inf\") or v == float(\"-inf\")):\n        fail(\"non-finite value for %d: %s\" % (type(v), v))\n    return v\n\n\"%d\" % finite_int(x)","typeGuard":"def is_finite_number(v):\n    return type(v) == \"int\" or (type(v) == \"float\" and v == v and v != float(\"inf\") and v != float(\"-inf\"))","tryCatchPattern":null,"preventionTips":["Guard divisors before computing ratios that feed %d.","Use %g/%f for anything that may legitimately be non-integral or non-finite."],"tags":["starlark","format","float","nan","infinity"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}