Reputation: 65
I have a python3.4 project and I recently decided to use mypy for better understanding.
This chunk of code works but checking with mypy pops out an error :
import zipfile
def zip_to_txt(zip: typing.IO[bytes]) -> BytesIO:
zz = zipfile.ZipFile(zip)
output = BytesIO()
for line, info in enumerate(zz.filelist):
date = "%d-%02d-%02d %02d:%02d:%02d" % info.date_time[:6]
output.write(str.encode("%-46s %s %12d\n" % (info.filename, date, info.file_size)))
output.seek(0, 0)
return output
The error :
PyPreviewGenerator/file_converter.py:170: error: "ZipFile" has no attribute "filelist"
(corresponds to this line : for line, info in enumerate(zz.filelist):
)
But when I look inside the ZipFile class, I can clearly see that the attribute exists.
So why does the error occurs ? and is there a way I can resolve it ?
Upvotes: 4
Views: 1561
Reputation: 64248
In short, the reason is because the filelist
attribute is not documented within Typeshed, the collection of type stubs for the stdlib/various 3rd party libraries. You can see this for yourself here.
Why is filelist
not included? Well, because it doesn't actually appear to be a documented part of the API. If you search through the document, you'll see filelist
is not mentioned anywhere.
Instead, you should call the infolist()
method, which returns exactly what you want (see implementation here if you're curious). You'll notice infolist()
is indeed listed within typeshed.
Upvotes: 3