Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TarFile.open is wrongly typed #12549

Open
Ravencentric opened this issue Aug 18, 2024 · 1 comment
Open

TarFile.open is wrongly typed #12549

Ravencentric opened this issue Aug 18, 2024 · 1 comment
Labels
stubs: false negative Type checkers do not report an error, but should topic: io I/O related issues

Comments

@Ravencentric
Copy link

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'
@AlexWaygood AlexWaygood added stubs: false negative Type checkers do not report an error, but should topic: io I/O related issues labels Aug 18, 2024
@srittau
Copy link
Collaborator

srittau commented Aug 18, 2024

Possibly fixed by #12181.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false negative Type checkers do not report an error, but should topic: io I/O related issues
Projects
None yet
Development

No branches or pull requests

3 participants