Python Mypy Attribute Error

I have a python3.4 project and recently decided to use mypy for better understanding.

This piece of code works, but checking with mypy throws 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

      

Mistake:

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 at the ZipFile class, I can clearly see that this attribute exists.
So why does the error occur? and is there any way to resolve it?

+3


source to share


1 answer


In short, the reason is that the attribute is filelist

not documented in the typeset, a collection of type titles for stdlibs / various third party developers. You can see it for yourself here .

Why filelist

not included? Good, because it's not actually a documented part of the API . If you look at the document, you will see that filelist

it is not mentioned anywhere.



Instead, you should call a method infolist()

that returns exactly what you want (see implementation here if you're interested). You will notice that infolist()

indeed the listed>.

+2


source







All Articles