{"record":{"id":"be2b6f46331bd745","repo":"TheAlgorithms/Python","slug":"expected-int-found-type-r-input-type-g-input","errorCode":null,"errorMessage":"Expected int, found {type(r_input), type(g_input), type(b_input)}","messagePattern":"Expected int, found (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/rgb_cmyk_conversion.py","lineNumber":45,"sourceCode":"    (0, 0, 0, 100)\r\n\r\n    >>> rgb_to_cmyk(255, 0, 0)  # red\r\n    (0, 100, 100, 0)\r\n\r\n    >>> rgb_to_cmyk(0, 255, 0)  # green\r\n    (100, 0, 100, 0)\r\n\r\n    >>> rgb_to_cmyk(0, 0, 255)    # blue\r\n    (100, 100, 0, 0)\r\n    \"\"\"\r\n\r\n    if (\r\n        not isinstance(r_input, int)\r\n        or not isinstance(g_input, int)\r\n        or not isinstance(b_input, int)\r\n    ):\r\n        msg = f\"Expected int, found {type(r_input), type(g_input), type(b_input)}\"\r\n        raise ValueError(msg)\r\n\r\n    if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256:\r\n        raise ValueError(\"Expected int of the range 0..255\")\r\n\r\n    # changing range from 0..255 to 0..1\r\n    r = r_input / 255\r\n    g = g_input / 255\r\n    b = b_input / 255\r\n\r\n    k = 1 - max(r, g, b)\r\n\r\n    if k == 1:  # pure black\r\n        return 0, 0, 0, 100\r\n\r\n    c = round(100 * (1 - r - k) / (1 - k))\r\n    m = round(100 * (1 - g - k) / (1 - k))\r\n    y = round(100 * (1 - b - k) / (1 - k))\r\n    k = round(100 * k)\r","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/rgb_cmyk_conversion.py#L27-L63","documentation":"Raised by rgb_to_cmyk() in conversions/rgb_cmyk_conversion.py when any of r_input, g_input, b_input is not an int (isinstance check per channel). The message interpolates a tuple of the three types, e.g. \"Expected int, found (<class 'float'>, <class 'int'>, <class 'int'>)\". Note bool is an int subclass and passes; floats like 255.0 fail.","triggerScenarios":"Calling rgb_to_cmyk(255.0, 0, 0) with floats, passing strings ('255', 0, 0), or values read from JSON/pandas that were parsed as float.","commonSituations":"Color values from UI sliders or image libraries delivered as floats; numpy integer types (np.int64 fails isinstance(x, int) on some versions); mixing normalized 0..1 floats with 0..255 ints.","solutions":["Cast to int at the call site: rgb_to_cmyk(int(r), int(g), int(b)) after range-checking.","If values are 0..1 floats, rescale first: round(v * 255).","For numpy scalars, convert with int(x) or .item() before calling."],"exampleFix":"# before\nrgb_to_cmyk(255.0, 0.0, 0.0)  # ValueError: Expected int, found (<class 'float'>, ...)\n\n# after\nrgb_to_cmyk(int(255.0), int(0.0), int(0.0))  # (0, 100, 100, 0)","handlingStrategy":"type-guard","validationCode":"if not all(isinstance(c, int) and not isinstance(c, bool) for c in (r, g, b)):\n    r, g, b = int(r), int(g), int(b)\nrgb_to_cmyk(r, g, b)","typeGuard":"def are_int_channels(channels: tuple) -> bool:\n    return all(\n        isinstance(c, int) and not isinstance(c, bool) and 0 <= c <= 255\n        for c in channels\n    )","tryCatchPattern":"try:\n    rgb_to_cmyk(r, g, b)\nexcept ValueError as e:\n    if 'Expected int' in str(e):\n        rgb_to_cmyk(int(r), int(g), int(b))\n    else:\n        raise","preventionTips":["Cast floats (including numpy scalars via .item()) to int at the boundary","Rescale 0..1 normalized floats by 255 before calling","Watch out: bool passes the isinstance(int) check"],"tags":["python","type-error","color","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}