mypy: Fix new issues in qtnetworkdownloads/webkit.mhtml/adblock

This commit is contained in:
Florian Bruhin 2019-10-21 22:36:58 +02:00
parent d9a68d606c
commit 34ed37a5b0
6 changed files with 30 additions and 8 deletions

View File

@ -14,14 +14,13 @@ disallow_untyped_decorators = True
# disallow_untyped_defs = True
## https://github.com/python/mypy/issues/5954
# disallow_incomplete_defs = True
# check_untyped_defs = True
check_untyped_defs = True
# no_implicit_optional = True
# warn_return_any = True
warn_unreachable = True
# Other strictness flags
strict_equality = True
check_untyped_defs = True
# Output
show_error_codes = True

View File

@ -799,7 +799,8 @@ class Application(QApplication):
log.init.debug("Initializing application...")
self.launch_time = datetime.datetime.now()
self.focusObjectChanged.connect(self.on_focus_object_changed)
self.focusObjectChanged.connect( # type: ignore
self.on_focus_object_changed)
self.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
@pyqtSlot(QObject)

View File

@ -455,8 +455,9 @@ class AbstractDownloadItem(QObject):
self.successful = False
self.fileobj = UnsupportedAttribute(
) # type: typing.Union[UnsupportedAttribute, typing.IO[bytes]]
self.raw_headers = UnsupportedAttribute()
) # type: typing.Union[UnsupportedAttribute, typing.IO[bytes], None]
self.raw_headers = UnsupportedAttribute(
) # type: typing.Union[UnsupportedAttribute, typing.Dict[bytes, bytes]]
self._filename = None
self._dead = False

View File

@ -23,6 +23,7 @@ import io
import os.path
import shutil
import functools
import typing
import attr
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QTimer
@ -90,8 +91,8 @@ class DownloadItem(downloads.AbstractDownloadItem):
reply: The QNetworkReply to download.
"""
super().__init__(parent=manager)
self.fileobj = None
self.raw_headers = {}
self.fileobj = None # type: typing.Optional[typing.IO[bytes]]
self.raw_headers = {} # type: typing.Dict[bytes, bytes]
self._autoclose = True
self._manager = manager
@ -106,6 +107,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
def _create_fileobj(self):
"""Create a file object using the internal filename."""
assert self._filename is not None
try:
fileobj = open(self._filename, 'wb')
except OSError as e:
@ -177,6 +179,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
"""Retry a failed download."""
assert self.done
assert not self.successful
assert self._retry_info is not None
new_reply = self._retry_info.manager.get(self._retry_info.request)
new_download = self._manager.fetch(new_reply,
suggested_filename=self.basename)
@ -208,6 +211,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
def _ask_create_parent_question(self, title, msg,
force_overwrite, remember_directory):
assert self._filename is not None
no_action = functools.partial(self.cancel, remove_data=False)
url = 'file://{}'.format(os.path.dirname(self._filename))
message.confirm_async(title=title, text=msg,
@ -224,6 +228,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
Args:
fileobj: A file-like object.
"""
assert self._reply is not None
if self.fileobj is not None: # pragma: no cover
raise ValueError("fileobj was already set! Old: {}, new: "
"{}".format(self.fileobj, fileobj))
@ -252,6 +257,8 @@ class DownloadItem(downloads.AbstractDownloadItem):
def _finish_download(self):
"""Write buffered data to disk and finish the QNetworkReply."""
assert self._reply is not None
assert self.fileobj is not None
log.downloads.debug("Finishing download...")
if self._reply.isOpen():
self.fileobj.write(self._reply.readAll())
@ -319,6 +326,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
@pyqtSlot()
def _on_read_timer_timeout(self):
"""Read some bytes from the QNetworkReply periodically."""
assert self._reply is not None
if not self._reply.isOpen():
raise OSError("Reply is closed!")
data = self._reply.read(1024)
@ -340,6 +348,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
Return:
True if the download was redirected, False otherwise.
"""
assert self._reply is not None
redirect = self._reply.attribute(
QNetworkRequest.RedirectionTargetAttribute)
if redirect is None or redirect.isEmpty():
@ -357,8 +366,11 @@ class DownloadItem(downloads.AbstractDownloadItem):
log.downloads.debug("{}: Handling redirect".format(self))
self._redirects += 1
new_request.setUrl(new_url)
old_reply = self._reply
assert old_reply is not None
old_reply.finished.disconnect(self._on_reply_finished)
self._read_timer.stop()
self._reply = None
if self.fileobj is not None:
@ -374,6 +386,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
def _uses_nam(self, nam):
"""Check if this download uses the given QNetworkAccessManager."""
assert self._retry_info is not None
running_nam = self._reply is not None and self._reply.manager() is nam
# user could request retry after tab is closed.
retry_nam = (self.done and (not self.successful) and

View File

@ -189,7 +189,7 @@ class MHTMLWriter:
self.root_content = root_content
self.content_location = content_location
self.content_type = content_type
self._files = {} # type: typing.Mapping[QUrl, _File]
self._files = {} # type: typing.MutableMapping[QUrl, _File]
def add_file(self, location, content, content_type=None,
transfer_encoding=E_QUOPRI):
@ -346,6 +346,8 @@ class _Downloader:
Args:
url: The file to download as QUrl.
"""
assert self.writer is not None
if url.scheme() not in ['http', 'https']:
return
# Prevent loading an asset twice
@ -385,6 +387,8 @@ class _Downloader:
url: The original url of the asset as QUrl.
item: The DownloadItem given by the DownloadManager
"""
assert self.writer is not None
self.pending_downloads.remove((url, item))
mime = item.raw_headers.get(b'Content-Type', b'')
@ -435,6 +439,7 @@ class _Downloader:
url: The original url of the asset as QUrl.
item: The DownloadItem given by the DownloadManager.
"""
assert self.writer is not None
try:
self.pending_downloads.remove((url, item))
except KeyError:
@ -465,6 +470,8 @@ class _Downloader:
def _finish_file(self):
"""Save the file to the filename given in __init__."""
assert self.writer is not None
if self._finished_file:
log.downloads.debug("finish_file called twice, ignored!")
return

View File

@ -304,6 +304,7 @@ class HostBlocker:
self._done_count += 1
assert not isinstance(download.fileobj,
downloads.UnsupportedAttribute)
assert download.fileobj is not None
try:
self._merge_file(download.fileobj)
finally: