{"record":{"id":"d595cb54f9e230e8","repo":"pytest-dev/pytest","slug":"don-t-know-how-to-compute-hashtype-r-hash","errorCode":null,"errorMessage":"Don't know how to compute {hashtype!r} hash","messagePattern":"Don't know how to compute (.+?) hash","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/_py/path.py","lineNumber":629,"sourceCode":"            else:\n                error.checked_call(os.rmdir, self.strpath)\n        else:\n            if iswin32:\n                self.chmod(0o700)\n            error.checked_call(os.remove, self.strpath)\n\n    def computehash(self, hashtype=\"md5\", chunksize=524288):\n        \"\"\"Return hexdigest of hashvalue for this file.\"\"\"\n        try:\n            try:\n                import hashlib as mod\n            except ImportError:\n                if hashtype == \"sha1\":\n                    hashtype = \"sha\"\n                mod = __import__(hashtype)\n            hash = getattr(mod, hashtype)()\n        except (AttributeError, ImportError):\n            raise ValueError(f\"Don't know how to compute {hashtype!r} hash\")\n        f = self.open(\"rb\")\n        try:\n            while 1:\n                buf = f.read(chunksize)\n                if not buf:\n                    return hash.hexdigest()\n                hash.update(buf)\n        finally:\n            f.close()\n\n    def new(self, **kw):\n        \"\"\"Create a modified version of this path.\n        the following keyword arguments modify various path parts::\n\n          a:/some/path/to/a/file.ext\n          xx                           drive\n          xxxxxxxxxxxxxxxxx            dirname\n                            xxxxxxxx   basename","sourceCodeStart":611,"sourceCodeEnd":647,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/_py/path.py#L611-L647","documentation":"LocalPath.computehash tries to obtain a hash constructor by importing hashlib and doing getattr(hashlib, hashtype); if hashlib is unavailable it falls back to __import__(hashtype). If both fail (unknown algorithm name, or hashlib lacks that attribute), pytest raises ValueError. The accepted names are whatever hashlib exposes (md5, sha1, sha256, sha512, blake2b, etc.) on the running Python version.","triggerScenarios":"Calling path.computehash('md6'), path.computehash('sha3-256') (should be sha3_256), path.computehash('crc32'), or any string not in hashlib.algorithms_available. Triggered at LocalPath.computehash (src/_pytest/_py/path.py:620-629).","commonSituations":"Using underscores vs hyphens incorrectly (sha3_256 vs sha3-256); requesting an algorithm not built into the Python interpreter (e.g., OpenSSL build lacks it); typos in the algorithm name.","solutions":["Use a name present in hashlib.algorithms_available: `path.computehash('sha256')`.","Validate first: `import hashlib; assert hashtype in hashlib.algorithms_available`.","Prefer hashlib directly (hashlib.file_digest or a manual loop) over the legacy py.path API for new code."],"exampleFix":"# before\npath.computehash('sha3-256')\n# after\npath.computehash('sha3_256')","handlingStrategy":"validation","validationCode":"import hashlib\ndef is_known_hash(name: str) -> bool:\n    return name in hashlib.algorithms_available\n# usage\nhashtype = 'sha256'\nassert is_known_hash(hashtype), f'unknown hash: {hashtype}'\ndigest = path.computehash(hashtype)","typeGuard":"import hashlib\ndef is_valid_hash_name(name: str) -> bool:\n    return isinstance(name, str) and name in hashlib.algorithms_available","tryCatchPattern":null,"preventionTips":["Check hashlib.algorithms_available before calling computehash.","Use underscores not hyphens (sha3_256, not sha3-256).","Prefer hashlib.file_digest over the legacy py.path API for new code."],"tags":["pytest","localpath","py-path","hash","validation","legacy"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}