typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

`TarFile.open` is wrongly typed

Open Ravencentric opened this issue 1 year ago • 1 comments

All the code snippets in the following cases passed mypy . --strict but failed at runtime.

Case 1

import tarfile

with tarfile.open("filename.tar", "w", compresslevel=None) as tar:
    tar.add("readme.txt")
Traceback (most recent call last):
  File "c:\Users\raven\Documents\GitHub\tarfiletest\test.py", line 3, in <module>
    with tarfile.open("filename.tar", "w", compresslevel=None) as tar:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1844, in open
    return cls.taropen(name, mode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1854, in taropen
    return cls(name, mode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TarFile.__init__() got an unexpected keyword argument 'compresslevel'

Case 2

import tarfile

with tarfile.open("filename.tar.gz", "w:gz", compresslevel=None) as tar:
    tar.add("readme.txt")
Traceback (most recent call last):
  File "c:\Users\raven\Documents\GitHub\tarfiletest\test.py", line 3, in <module>
    with tarfile.open("filename.tar.gz", "w:gz", compresslevel=None) as tar:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1822, in open
    return func(name, filemode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1870, in gzopen
    fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\gzip.py", line 220, in __init__
    self.compress = zlib.compressobj(compresslevel,
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object cannot be interpreted as an integer

Case 3

import tarfile

with tarfile.open("filename.tar.xz", "w:xz", compresslevel=None) as tar:
    tar.add("readme.txt")
Traceback (most recent call last):
  File "c:\Users\raven\Documents\GitHub\tarfiletest\test.py", line 3, in <module>
    with tarfile.open("filename.tar.xz", "w:xz", compresslevel=None) as tar:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1822, in open
    return func(name, filemode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1933, in xzopen
    t = cls.taropen(name, mode, fileobj, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1854, in taropen
    return cls(name, mode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TarFile.__init__() got an unexpected keyword argument 'compresslevel'

Case 4

import tarfile

with tarfile.open("filename.tar.bz2", "w:bz2", compresslevel=None) as tar:
    tar.add("readme.txt")
Traceback (most recent call last):
  File "c:\Users\raven\Documents\GitHub\tarfiletest\test.py", line 3, in <module>
    with tarfile.open("filename.tar.bz2", "w:bz2", compresslevel=None) as tar:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1822, in open
    return func(name, filemode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\tarfile.py", line 1902, in bz2open
    fileobj = BZ2File(fileobj or name, mode, compresslevel=compresslevel)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\raven\AppData\Local\Programs\Python\Python312\Lib\bz2.py", line 59, in __init__
    if not (1 <= compresslevel <= 9):
            ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'int' and 'NoneType'

Ravencentric avatar Aug 18 '24 14:08 Ravencentric

Possibly fixed by #12181.

srittau avatar Aug 18 '24 15:08 srittau