Skip to content

Commit

Permalink
Fix up core.py for mypy usage
Browse files Browse the repository at this point in the history
Add a mypy config to pyproject.toml
  • Loading branch information
gazpachoking committed Feb 4, 2022
1 parent 9ebbaf3 commit e0770be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
36 changes: 21 additions & 15 deletions deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
import shutil
import tempfile
import threading
import typing
from base64 import b64decode, b64encode
from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.request import URLError, urlopen
from urllib.error import URLError
from urllib.request import urlopen

from twisted.internet import defer, reactor, task
import twisted.internet.reactor
from twisted.internet import defer, task
from twisted.internet.base import ReactorBase
from twisted.web.client import Agent, readBody

import deluge.common
Expand Down Expand Up @@ -54,6 +58,8 @@
)
from deluge.httpdownloader import download_file

reactor = typing.cast(ReactorBase, twisted.internet.reactor)

log = logging.getLogger(__name__)

DEPR_SESSION_STATUS_KEYS = {
Expand Down Expand Up @@ -313,12 +319,8 @@ def _save_session_state(self):
log.info('Restoring backup of %s from: %s', filename, filepath_bak)
shutil.move(filepath_bak, filepath)

def _load_session_state(self) -> dict:
"""Loads the libtorrent session state
Returns:
A libtorrent sesion state, empty dict if unable to load it.
"""
def _load_session_state(self):
"""Loads the libtorrent session state"""
filename = 'session.state'
filepath = get_config_dir(filename)
filepath_bak = filepath + '.bak'
Expand Down Expand Up @@ -400,7 +402,11 @@ def check_new_release(self):
# Exported Methods
@export
def add_torrent_file_async(
self, filename: str, filedump: str, options: dict, save_state: bool = True
self,
filename: str,
filedump: Union[str, bytes],
options: dict,
save_state: bool = True,
) -> defer.Deferred[Optional[str]]:
"""Adds a torrent file to the session asynchronously.
Expand Down Expand Up @@ -456,7 +462,7 @@ def on_metadata(result, result_d):

d = self.torrentmanager.prefetch_metadata(magnet, timeout)
# Use a separate callback chain to handle existing prefetching magnet.
result_d = defer.Deferred()
result_d: defer.Deferred = defer.Deferred()
d.addBoth(on_metadata, result_d)
return result_d

Expand Down Expand Up @@ -515,7 +521,7 @@ def add_torrents():
errors.append(ex)
defer.returnValue(errors)

return task.deferLater(reactor, 0, add_torrents)
return task.deferLater(reactor, 0, add_torrents) # type: ignore

@export
def add_torrent_url(
Expand Down Expand Up @@ -673,7 +679,7 @@ def pause_torrent(self, torrent_id: str) -> None:
@export
def pause_torrents(self, torrent_ids: List[str] = None) -> None:
"""Pauses a list of torrents"""
if not torrent_ids:
if torrent_ids is None:
torrent_ids = self.torrentmanager.get_torrent_list()
for torrent_id in torrent_ids:
self.pause_torrent(torrent_id)
Expand Down Expand Up @@ -724,7 +730,7 @@ def resume_torrent(self, torrent_id: str) -> None:
@export
def resume_torrents(self, torrent_ids: List[str] = None) -> None:
"""Resumes a list of torrents"""
if not torrent_ids:
if torrent_ids is None:
torrent_ids = self.torrentmanager.get_torrent_list()
for torrent_id in torrent_ids:
self.resume_torrent(torrent_id)
Expand Down Expand Up @@ -770,9 +776,9 @@ def get_torrent_status(
all_keys=not keys,
)

@export
@export # type: ignore
@defer.inlineCallbacks
def get_torrents_status(
def get_torrents_status( # type: ignore
self, filter_dict: dict, keys: List[str], diff: bool = False
) -> dict:
"""returns all torrents , optionally filtered by filter_dict."""
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ skip-string-normalization = true

[tool.isort]
profile = "black"

[tool.mypy]
python_version = 3.6
namespace_packages = true
plugins = ["mypy_zope:plugin"]
allow_redefinition = true

0 comments on commit e0770be

Please sign in to comment.