I'm getting the following mypy error for the code below. How can I properly cast the response object so that mypy is happy with me passing it to the shutil.copyfileobj method?
error:Argument 1 to "copyfileobj" has incompatible type "Union[HTTPResponse, BinaryIO]"; expected IO[Any]
The following code streams the response from a web request to a file.
request = urllib.request.Request(get_file_url, headers=self.data_api_headers)
with urllib.request.urlopen(request) as response:
with open(export_file_path, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
This was a bug in typeshed
the HTTPResponse
stub didn't extend from BinaryIO
and therefore wasn't considered a candidate for functions taking IO[bytes]
-- this has been fixed
This fix should land in the next release of mypy
(should be 0.620
if I'm guessing their version scheme correctly). Alternatively, you can run mypy
with --custom-typeshed-dir
to get the changes sooner
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.