4

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 ?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did not know that it needs to be used as it is documented to work with mypy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.