{"record":{"id":"83acd170cd8a7e24","repo":"redis/redis-py","slug":"genpass-optionally-accepts-a-bits-argument-betwee","errorCode":null,"errorMessage":"genpass optionally accepts a bits argument, between 0 and 4096.","messagePattern":"genpass optionally accepts a bits argument, between 0 and 4096\\.","errorType":"exception","errorClass":"DataError","httpStatus":null,"severity":"error","filePath":"redis/commands/core.py","lineNumber":209,"sourceCode":"    ) -> Awaitable[bytes | str]: ...\n\n    def acl_genpass(self, bits: int | None = None, **kwargs) -> (\n        bytes | str\n    ) | Awaitable[bytes | str]:\n        \"\"\"Generate a random password value.\n        If ``bits`` is supplied then use this number of bits, rounded to\n        the next multiple of 4.\n        See: https://redis.io/commands/acl-genpass\n        \"\"\"\n        pieces = []\n        if bits is not None:\n            try:\n                b = int(bits)\n                if b < 0 or b > 4096:\n                    raise ValueError\n                pieces.append(b)\n            except ValueError:\n                raise DataError(\n                    \"genpass optionally accepts a bits argument, between 0 and 4096.\"\n                )\n        return self.execute_command(\"ACL GENPASS\", *pieces, **kwargs)\n\n    @overload\n    def acl_getuser(\n        self: SyncClientProtocol, username: str, **kwargs\n    ) -> ACLGetUserData: ...\n\n    @overload\n    def acl_getuser(\n        self: AsyncClientProtocol, username: str, **kwargs\n    ) -> Awaitable[ACLGetUserData]: ...\n\n    def acl_getuser(\n        self, username: str, **kwargs\n    ) -> ACLGetUserData | Awaitable[ACLGetUserData]:\n        \"\"\"","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/core.py#L191-L227","documentation":"Raised by Redis.acl_genpass() when the optional `bits` argument cannot be converted to an integer, or is outside the inclusive range 0..4096. The library catches a ValueError from int() conversion or the explicit range check and re-raises it as a DataError with a descriptive message. This is the same constraint the server enforces for ACL GENPASS.","triggerScenarios":"Calling client.acl_genpass(bits) where bits is a non-numeric string (e.g. 'random'), None passed via keyword incorrectly, a negative int, an int greater than 4096, or a float like 100.5 (int() truncates but 4097/−1 still fail the range check).","commonSituations":"Generating ACL passwords programmatically with a user-supplied bit length; passing a config value that was loaded as a string and not coerced to int; assuming default is 256 but supplying 0 expecting 'unlimited'.","solutions":["Ensure `bits` is an int in [0, 4096] before calling; coerce with int(bits) and clamp/validate upstream.","Omit `bits` entirely to use the server default.","Validate with a small helper that raises your own configuration error with context."],"exampleFix":"# before\nclient.acl_genpass(bits=8192)\n# after\nbits = int(config_bits)\nif not 0 <= bits <= 4096:\n    raise ValueError(f'bits out of range: {bits}')\nclient.acl_genpass(bits=bits)","handlingStrategy":"validation","validationCode":"def safe_acl_genpass(client, bits=None):\n    if bits is not None:\n        b = int(bits)\n        if not 0 <= b <= 4096:\n            raise ValueError(f'bits must be in [0, 4096], got {b}')\n    return client.acl_genpass(bits=b if bits is None else int(bits))","typeGuard":"def is_valid_genpass_bits(bits) -> bool:\n    try:\n        return 0 <= int(bits) <= 4096\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"from redis.exceptions import DataError\ntry:\n    token = client.acl_genpass(bits=bits)\nexcept DataError as e:\n    if 'bits argument' in str(e):\n        token = client.acl_genpass()  # fall back to server default\n    else:\n        raise","preventionTips":["Coerce env/config-sourced bit values to int before calling.","Centralize ACL password generation in a helper that validates the range.","Omit bits to use the server default rather than guessing."],"tags":["acl","validation","acl-genpass","input-validation","range-check"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}