{"record":{"id":"f6d22606cb705a4e","repo":"pypa/pip","slug":"cannot-restore-version-from-state-r","errorCode":null,"errorMessage":"Cannot restore Version from {state!r}","messagePattern":"Cannot restore Version from (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/version.py","lineNumber":833,"sourceCode":"                    self._pre = slot_dict.get(\"_pre\")\n                    self._post = slot_dict.get(\"_post\")\n                    self._dev = slot_dict.get(\"_dev\")\n                    self._local = slot_dict.get(\"_local\")\n                    return\n        if isinstance(state, dict):\n            # Old format (packaging <= 25.x, no __slots__): state is a plain\n            # dict with \"_version\" (_Version NamedTuple) and \"_key\" entries.\n            version_nt = state.get(\"_version\")\n            if version_nt is not None:\n                self._epoch = version_nt.epoch\n                self._release = version_nt.release\n                self._pre = version_nt.pre\n                self._post = version_nt.post\n                self._dev = version_nt.dev\n                self._local = version_nt.local\n                return\n\n        raise TypeError(f\"Cannot restore Version from {state!r}\")\n\n    @property\n    @_deprecated(\"Version._version is private and will be removed soon\")\n    def _version(self) -> _Version:\n        return _Version(\n            self._epoch, self._release, self._dev, self._pre, self._post, self._local\n        )\n\n    @_version.setter\n    @_deprecated(\"Version._version is private and will be removed soon\")\n    def _version(self, value: _Version) -> None:\n        self._epoch = value.epoch\n        self._release = value.release\n        self._dev = value.dev\n        self._pre = value.pre\n        self._post = value.post\n        self._local = value.local\n        self._key_cache = None","sourceCodeStart":815,"sourceCodeEnd":851,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/packaging/version.py#L815-L851","documentation":"Raised by Version.__setstate__ during unpickling when the saved state does not match any known pickle format: the 6-tuple (epoch, release, pre, post, dev, local) used by packaging 26.2+, the 2-tuple (None, slot_dict) from 26.0-26.1, or the legacy dict format from packaging <= 25.x. If none of these shapes is recognized, unpickling cannot proceed and TypeError is raised. This protects against restoring a Version from corrupted or foreign pickle data.","triggerScenarios":"Unpickling a Version object from a pickle file/stream whose state tuple/dict was manually constructed, truncated, or written by a completely different (non-packaging) serialization scheme. Also possible if a future format change introduces a shape not yet handled.","commonSituations":"Loading a stale cache or joblib/cloudpickle artifact that contains a Version serialized by an incompatible or much older/newer packaging version; passing a hand-built state to a Version via copy.copy/copy.deepcopy with a patched __reduce__.","solutions":["Re-serialize the Version objects with the current packaging version to regenerate a compatible pickle.","Avoid pickling Version objects across packaging major versions; store the version as a string and reconstruct with Version(str).","If migrating, decode old pickles by extracting the version string and re-creating the Version instance."],"exampleFix":"// before\n# old cache: pickle.load(f) containing pre-26.2 Version with unknown state\n// after\n# store version strings instead of Version objects in the cache\nwith open(cache, 'w') as f:\n    json.dump(str(v), f)\n# later: v = Version(pickle.load_or_json_string)","handlingStrategy":"validation","validationCode":"# Before unpickling, store/retrieve version strings instead of Version objects\n# v = Version(pickle.load(f))  # reconstruct from string, not unpickle the object","typeGuard":null,"tryCatchPattern":"try:\n    obj = pickle.load(f)\nexcept TypeError as e:\n    if 'Cannot restore Version' in str(e):\n        # regenerate from a stored string representation\n        obj = None","preventionTips":["Don't pickle Version objects across packaging major versions.","Store versions as strings in caches/databases and reconstruct at load time.","Invalidate caches when upgrading the packaging library."],"tags":["versioning","pickle","serialization","packaging"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}