{"id":"c7dd3c078c17e2a0","repo":"pypa/pip","slug":"unknown-format-for-r","errorCode":null,"errorMessage":"Unknown format for %r","messagePattern":"Unknown format for %r","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/distlib/util.py","lineNumber":1271,"sourceCode":"        check_path(member.linkname, base=link_base)\n\n    dest_dir = os.path.abspath(dest_dir)\n    plen = len(dest_dir)\n    archive = None\n    if format is None:\n        if archive_filename.endswith(('.zip', '.whl')):\n            format = 'zip'\n        elif archive_filename.endswith(('.tar.gz', '.tgz')):\n            format = 'tgz'\n            mode = 'r:gz'\n        elif archive_filename.endswith(('.tar.bz2', '.tbz')):\n            format = 'tbz'\n            mode = 'r:bz2'\n        elif archive_filename.endswith('.tar'):\n            format = 'tar'\n            mode = 'r'\n        else:  # pragma: no cover\n            raise ValueError('Unknown format for %r' % archive_filename)\n    try:\n        if format == 'zip':\n            archive = ZipFile(archive_filename, 'r')\n            if check:\n                names = archive.namelist()\n                for name in names:\n                    check_path(name)\n        else:\n            archive = tarfile.open(archive_filename, mode)\n            if check:\n                for member in archive.getmembers():\n                    check_path(member.name)\n                    check_link(member)\n        if format != 'zip' and sys.version_info[0] < 3:\n            # See Python issue 17153. If the dest path contains Unicode,\n            # tarfile extraction fails on Python 2.x if a member path name\n            # contains non-ASCII characters - it leads to an implicit\n            # bytes -> unicode conversion using ASCII to decode.","sourceCodeStart":1253,"sourceCodeEnd":1289,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/distlib/util.py#L1253-L1289","documentation":"Raised by unarchive() when the archive format cannot be inferred and the caller did not pass an explicit format= argument. The extension is checked against ('.zip', '.tar.gz', '.tgz', '.tar.bz2', '.tbz', '.tar', '.whl'); if none match, ValueError 'Unknown format for %r' is raised.","triggerScenarios":"unarchive('data.xz'), unarchive('snapshot'), unarchive('file.tar.xz'), or calling unarchive on a path with no recognized extension while passing format=None.","commonSituations":"Renaming archives, downloading files whose extension was stripped, or newer compression formats (.tar.xz, .tar.zst) not in the recognized set.","solutions":["Pass the format explicitly: unarchive(path, dest, format='zip') or format='tgz' / 'tbz' / 'tar'.","Rename the file to a recognized extension before calling.","For .tar.xz/.tar.zst, extract with Python's tarfile.open(path, 'r:xz'/'r:zst') directly instead of unarchive()."],"exampleFix":"// before\nunarchive('snapshot', dest)  # no extension\n// after\nunarchive('snapshot', dest, format='tgz')","handlingStrategy":"validation","validationCode":"import os\nKNOWN = ('.zip', '.tar.gz', '.tgz', '.tar.bz2', '.tbz', '.tar', '.whl')\ndef infer_format(path):\n    low = path.lower()\n    for ext in KNOWN:\n        if low.endswith(ext):\n            return {'zip':'zip','.whl':'zip','.tgz':'tgz','.tar.gz':'tgz','.tbz':'tbz','.tar.bz2':'tbz','.tar':'tar'}[ext]\n    return None\n\ndef safe_unarchive(path, dest, **kw):\n    fmt = kw.pop('format', None) or infer_format(path)\n    if fmt is None:\n        raise ValueError('cannot infer archive format for %r; pass format=' % path)\n    from distlib.util import unarchive\n    return unarchive(path, dest, format=fmt, **kw)","typeGuard":null,"tryCatchPattern":"from distlib.util import unarchive\ntry:\n    unarchive(path, dest)\nexcept ValueError as e:\n    if 'Unknown format' in str(e):\n        unarchive(path, dest, format='tgz')  # pick correct format explicitly\n    else:\n        raise","preventionTips":["Always pass an explicit format= when the archive lacks a recognized extension.","Rename downloaded archives to their true extension before calling unarchive().","Use tarfile/zipfile directly for formats distlib does not recognize (.tar.xz, .tar.zst)."],"tags":["archive","format","packaging","validation"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}