{"record":{"id":"a9579d3c56cc52cc","repo":"TheAlgorithms/Python","slug":"n-should-be-an-integer-greater-than-0","errorCode":null,"errorMessage":"n should be an integer greater than 0.","messagePattern":"n should be an integer greater than 0\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"other/least_recently_used.py","lineNumber":46,"sourceCode":"    >>> lru_cache\n    LRUCache(4) => [5, 4, 'A', 3]\n\n    \"\"\"\n\n    dq_store: deque[T]  # Cache store of keys\n    key_reference: set[T]  # References of the keys in cache\n    _MAX_CAPACITY: int = 10  # Maximum capacity of cache\n\n    def __init__(self, n: int) -> None:\n        \"\"\"Creates an empty store and map for the keys.\n        The LRUCache is set the size n.\n        \"\"\"\n        self.dq_store = deque()\n        self.key_reference = set()\n        if not n:\n            LRUCache._MAX_CAPACITY = sys.maxsize\n        elif n < 0:\n            raise ValueError(\"n should be an integer greater than 0.\")\n        else:\n            LRUCache._MAX_CAPACITY = n\n\n    def refer(self, x: T) -> None:\n        \"\"\"\n        Looks for a page in the cache store and adds reference to the set.\n        Remove the least recently used key if the store is full.\n        Update store to reflect recent access.\n        \"\"\"\n        if x not in self.key_reference:\n            if len(self.dq_store) == LRUCache._MAX_CAPACITY:\n                last_element = self.dq_store.pop()\n                self.key_reference.remove(last_element)\n        else:\n            self.dq_store.remove(x)\n\n        self.dq_store.appendleft(x)\n        self.key_reference.add(x)","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/least_recently_used.py#L28-L64","documentation":"Raised by LRUCache.__init__ when the capacity argument n is negative. Note the guard order: n == 0 (falsy) sets capacity to sys.maxsize (effectively unbounded), n < 0 raises, and any positive n sets the capacity. Also, _MAX_CAPACITY is a class attribute, so the last constructed instance overwrites the capacity for all instances.","triggerScenarios":"Calling LRUCache(-1) or LRUCache(-100). Separately, LRUCache(0) silently makes the cache unbounded, and constructing multiple caches with different n leaks the setting across instances because _MAX_CAPACITY is shared at class level.","commonSituations":"Computing capacity from configuration (e.g. size - 1 that can go negative) or from a subtraction that underflows; also multi-cache programs surprised by the shared class-level capacity.","solutions":["Pass a positive integer capacity: LRUCache(10)","Validate config-derived sizes: n = max(1, requested_size)","Be aware LRUCache(0) means unbounded (sys.maxsize), not zero-capacity","If you create several caches, avoid relying on per-instance capacity — _MAX_CAPACITY is class-wide in this implementation"],"exampleFix":"# before\ncache = LRUCache(user_configured_size)  # -1 -> ValueError\n\n# after\ncache = LRUCache(max(1, user_configured_size))","handlingStrategy":"validation","validationCode":"def valid_capacity(n) -> bool:\n    return isinstance(n, int) and n >= 0  # note: 0 means unbounded here","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp config-derived capacities: LRUCache(max(1, size))","Remember LRUCache(0) means unbounded and capacity is class-wide, not per-instance"],"tags":["cache","argument-validation","lru"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}