pytype icon indicating copy to clipboard operation
pytype copied to clipboard

Spurious wrong-arg-types; gzip.GzipFile not recognized as satisfying _typeshed.SupportsWrite[bytes]

Open nathanielmanistaatgoogle opened this issue 6 months ago • 0 comments

Over in this area of python.org we see the sample program

import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
    with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

When I throw pytype (version 2024.4.11) at this sample program, type-checking fails with

File "/Users/nathaniel/temp-2023-08-25-pytype/haha.py", line 5, in <module>: Function shutil.copyfileobj was called with the wrong arguments [wrong-arg-types]
         Expected: (fsrc, fdst: _typeshed.SupportsWrite[bytes], ...)
  Actually passed: (fsrc, fdst: gzip.GzipFile)
  
  Method write of protocol _typeshed.SupportsWrite[AnyStr] has the wrong signature in gzip.GzipFile:
  
  >> _typeshed.SupportsWrite[AnyStr] expects:
  def write(self: _typeshed.SupportsWrite[_T_contra], __s: _T_contra) -> object: ...
  
  >> gzip.GzipFile defines:
  def write(self: gzip.GzipFile, data: Buffer) -> int: ...

For more details, see https://google.github.io/pytype/errors.html#wrong-arg-types

Type-checking this sample program ought pass, right?