{"id":"9e4929051ae2235e","repo":"redis/redis-py","slug":"name","errorCode":null,"errorMessage":"{name}","messagePattern":"\\{name\\}","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"redis/commands/core.py","lineNumber":3303,"sourceCode":"                \"and ``persist`` are mutually exclusive.\"\n            )\n\n        exp_options: list[EncodableT] = extract_expire_flags(ex, px, exat, pxat)\n\n        if persist:\n            exp_options.append(\"PERSIST\")\n\n        return self.execute_command(\"GETEX\", name, *exp_options)\n\n    def __getitem__(self, name: KeyT):\n        \"\"\"\n        Return the value at key ``name``, raises a KeyError if the key\n        doesn't exist.\n        \"\"\"\n        value = self.get(name)\n        if value is not None:\n            return value\n        raise KeyError(name)\n\n    @overload\n    def getbit(self: SyncClientProtocol, name: KeyT, offset: int) -> int: ...\n\n    @overload\n    def getbit(\n        self: AsyncClientProtocol, name: KeyT, offset: int\n    ) -> Awaitable[int]: ...\n\n    def getbit(self, name: KeyT, offset: int) -> int | Awaitable[int]:\n        \"\"\"\n        Returns an integer indicating the value of ``offset`` in ``name``\n\n        For more information, see https://redis.io/commands/getbit\n        \"\"\"\n        return self.execute_command(\"GETBIT\", name, offset, keys=[name])\n\n    @overload","sourceCodeStart":3285,"sourceCodeEnd":3321,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/commands/core.py#L3285-L3321","documentation":"The bracket-access operator r[name] (CoreCommands.__getitem__) calls get() and raises KeyError(name) when the key is absent. This mirrors dict semantics so client['foo'] fails fast on missing keys instead of returning None. The exception's message/arg is the key name itself.","triggerScenarios":"r['missing_key'] where the key does not exist in Redis. Returns the value only when the key is present; otherwise raises KeyError with the key as the argument.","commonSituations":"Treating the Redis client like a dict in templating or config code; assuming a key was written earlier in the same flow; using bracket access in a hot loop without existence checks.","solutions":["Use r.get('key') which returns None for missing keys if that is the desired semantics.","Check existence first with r.exists('key') when you need to branch on presence.","Catch KeyError around bracket access if absence is a normal, recoverable case."],"exampleFix":"# before\nval = r['maybe_missing']  # raises KeyError if absent\n\n# after\nval = r.get('maybe_missing')  # None if absent\n# or explicit presence check:\nif r.exists('maybe_missing'):\n    val = r['maybe_missing']","handlingStrategy":"try-catch","validationCode":"# Prefer get() to avoid the exception entirely:\nval = r.get('maybe_missing')\nif val is None:\n    ...  # handle absence without raising","typeGuard":"def key_exists(client, key) -> bool:\n    return bool(client.exists(key))","tryCatchPattern":"try:\n    val = r['maybe_missing']\nexcept KeyError:\n    val = None  # or your default","preventionTips":["Use r.get(key) when absence is normal; reserve bracket access for when missing keys are a bug.","Check r.exists(key) before bracket access in conditional flows."],"tags":["redis","keyerror","getitem","missing-key","dict-semantics"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}