{"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":"validation","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/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/commands/core.py#L191-L227","documentation":"Raised by acl_genpass() as a DataError when the bits argument is supplied but is not coercible to int, or is outside the inclusive range 0..4096. The guard at redis/commands/core.py:202-211 wraps int conversion + range check in a try/except ValueError, re-raising as DataError (a RedisError subclass). The bound matches the Redis server's ACL GENPASS limit.","triggerScenarios":"Calling r.acl_genpass(bits) where bits is a non-numeric string, None handled fine (omitted), a float outside range, a negative int, an int > 4096, or something whose int() raises (e.g. 'foo', '16.5', [1]). Note int('16') works, but int('16.5') raises.","commonSituations":"Reading bits from config/env as a string without conversion; passing a bit count larger than the documented max; copy-paste from a tutorial that uses an outdated limit; off-by-one on a UI slider bound to this value.","solutions":["Pass an int in [0, 4096] (or None for the default 256-bit password).","Validate and clamp the input before calling: bits = max(0, min(4096, int(bits))).","If accepting user input as a string, confirm it is a clean integer literal before calling acl_genpass."],"exampleFix":"# before\nr.acl_genpass(bits=user_input)  # user_input='64.0' or 5000\n# after\nbits = int(user_input)\nif not 0 <= bits <= 4096:\n    raise ValueError('bits must be 0..4096')\nr.acl_genpass(bits=bits)","handlingStrategy":"validation","validationCode":"def valid_bits(bits):\n    if bits is None:\n        return None\n    b = int(bits)  # raises ValueError for bad input -> convert to DataError yourself\n    if not 0 <= b <= 4096:\n        raise ValueError('bits must be in [0, 4096]')\n    return b\nclient.acl_genpass(bits=valid_bits(bits))","typeGuard":"def is_valid_bits(bits) -> bool:\n    if bits is None:\n        return True\n    try:\n        return 0 <= int(bits) <= 4096\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"from redis.exceptions import DataError\ntry:\n    client.acl_genpass(bits=bits)\nexcept DataError:\n    client.acl_genpass(bits=None)  # fall back to default","preventionTips":["Coerce and range-check bits at the input boundary.","Treat config-sourced values as strings until validated."],"tags":["acl","validation","genpass","argument-error","range"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}