{"record":{"id":"10a8abd592e57a3b","repo":"pola-rs/polars","slug":"maxsize-cannot-be-negative-found-n","errorCode":null,"errorMessage":"`maxsize` cannot be negative; found {n}","messagePattern":"`maxsize` cannot be negative; found (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/cache.py","lineNumber":153,"sourceCode":"\n    def items(self) -> ItemsView[K, V]:\n        \"\"\"Return an iterable view of the cache's items (keys and values).\"\"\"\n        return self._items.items()\n\n    def keys(self) -> KeysView[K]:\n        \"\"\"Return an iterable view of the cache's keys.\"\"\"\n        return self._items.keys()\n\n    @property\n    def maxsize(self) -> int:\n        return self._max_size\n\n    @maxsize.setter\n    def maxsize(self, n: int) -> None:\n        \"\"\"Set new maximum cache size; cache is trimmed if value is smaller.\"\"\"\n        if n < 0:\n            msg = f\"`maxsize` cannot be negative; found {n}\"\n            raise ValueError(msg)\n        while len(self) > n:\n            self.popitem()\n        self._max_size = n\n\n    def pop(self, key: K, default: D | NoDefault = NO_DEFAULT) -> V | D:\n        \"\"\"\n        Remove specified key from the cache and return the associated value.\n\n        If the key is not found, `default` is returned (if given).\n        Otherwise, a KeyError is raised.\n        \"\"\"\n        if (item := self._items.pop(key, default)) is NO_DEFAULT:\n            msg = f\"{key!r} not found in cache\"\n            raise KeyError(msg)\n        return item\n\n    def popitem(self) -> tuple[K, V]:\n        \"\"\"Remove the least recently used value; raises KeyError if cache is empty.\"\"\"","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/cache.py#L135-L171","documentation":"The Cache.maxsize setter validates that the new maximum size is non-negative; a negative value raises ValueError immediately. The setter also trims the cache down to the new limit while len(cache) exceeds it.","triggerScenarios":"Assigning a negative number to the cache's maxsize property, e.g. `cache.maxsize = -1`, typically from an unvalidated config value or arithmetic that underflowed.","commonSituations":"Config files/env vars parsed without validation (e.g. \"maxsize\": -1); computing size from a negative budget; wiring UI input straight into maxsize.","solutions":["Pass a non-negative integer: `cache.maxsize = max(0, configured_size)`","Validate the configured value before assignment (clamp to >= 0 or reject)","Trace the source of the negative number (config parsing/subtraction bug) and fix it at the origin","Use 0 to disable caching rather than a negative sentinel"],"exampleFix":"// before\ncache.maxsize = config[\"cache_size\"]  # ValueError if -1\n// after\nsize = config.get(\"cache_size\", 128)\ncache.maxsize = max(0, int(size))","handlingStrategy":"validation","validationCode":"n = int(config.get('cache_size', 128))\nif n < 0:\n    raise ValueError('cache_size must be >= 0')\ncache.maxsize = n","typeGuard":"def valid_maxsize(n) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 0","tryCatchPattern":"try:\n    cache.maxsize = configured\nexcept ValueError:\n    cache.maxsize = 128  # safe default","preventionTips":["Clamp config-derived sizes with max(0, value) before assignment","Validate numeric config fields at startup, not at use site","Avoid negative sentinels to mean 'disabled'; use 0 or None semantics explicitly","Add unit tests for boundary config values (0, -1)"],"tags":["python","cache","validation","invalid-argument"],"backgroundTag":"value-out-of-range","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}