pytest-dev/pytest · error · ValueError

Don't know how to compute {hashtype!r} hash

Error message

Don't know how to compute {hashtype!r} hash

What it means

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.

Source

Thrown at src/_pytest/_py/path.py:629

            else:
                error.checked_call(os.rmdir, self.strpath)
        else:
            if iswin32:
                self.chmod(0o700)
            error.checked_call(os.remove, self.strpath)

    def computehash(self, hashtype="md5", chunksize=524288):
        """Return hexdigest of hashvalue for this file."""
        try:
            try:
                import hashlib as mod
            except ImportError:
                if hashtype == "sha1":
                    hashtype = "sha"
                mod = __import__(hashtype)
            hash = getattr(mod, hashtype)()
        except (AttributeError, ImportError):
            raise ValueError(f"Don't know how to compute {hashtype!r} hash")
        f = self.open("rb")
        try:
            while 1:
                buf = f.read(chunksize)
                if not buf:
                    return hash.hexdigest()
                hash.update(buf)
        finally:
            f.close()

    def new(self, **kw):
        """Create a modified version of this path.
        the following keyword arguments modify various path parts::

          a:/some/path/to/a/file.ext
          xx                           drive
          xxxxxxxxxxxxxxxxx            dirname
                            xxxxxxxx   basename

View on GitHub (pinned to 0d6fbdeffa)

Solutions

  1. Use a name present in hashlib.algorithms_available: `path.computehash('sha256')`.
  2. Validate first: `import hashlib; assert hashtype in hashlib.algorithms_available`.
  3. Prefer hashlib directly (hashlib.file_digest or a manual loop) over the legacy py.path API for new code.

Example fix

# before
path.computehash('sha3-256')
# after
path.computehash('sha3_256')
Defensive patterns

Strategy: validation

Validate before calling

import hashlib
def is_known_hash(name: str) -> bool:
    return name in hashlib.algorithms_available
# usage
hashtype = 'sha256'
assert is_known_hash(hashtype), f'unknown hash: {hashtype}'
digest = path.computehash(hashtype)

Type guard

import hashlib
def is_valid_hash_name(name: str) -> bool:
    return isinstance(name, str) and name in hashlib.algorithms_available

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/d595cb54f9e230e8. Report an issue: GitHub.