diff --git a/electrumabc/address.py b/electrumabc/address.py index b096d2981b4e..8190948653d3 100644 --- a/electrumabc/address.py +++ b/electrumabc/address.py @@ -28,7 +28,7 @@ import hashlib import struct from collections import namedtuple -from typing import Tuple, Union +from typing import Optional, Tuple, Union from . import cashaddr, networks from .bitcoin import ( @@ -394,7 +394,7 @@ def from_cashaddr_string( cls, string: str, *, - net: networks.AbstractNet = None, + net: Optional[networks.AbstractNet] = None, support_arbitrary_prefix: bool = False, ): """Construct from a cashaddress string. @@ -451,7 +451,7 @@ def from_string( cls, string: str, *, - net: networks.AbstractNet = None, + net: Optional[networks.AbstractNet] = None, support_arbitrary_prefix: bool = False, ): """Construct from an address string. diff --git a/electrumabc/base_wizard.py b/electrumabc/base_wizard.py index f5d21308d8ae..ed54a2e867b3 100644 --- a/electrumabc/base_wizard.py +++ b/electrumabc/base_wizard.py @@ -146,7 +146,7 @@ def do_upgrade(): do_upgrade, _("Upgrading wallet format..."), on_finished=on_finished ) - def run_task_without_blocking_gui(self, task, *, msg: str = None) -> Any: + def run_task_without_blocking_gui(self, task, *, msg: Optional[str] = None) -> Any: """Perform a task in a thread without blocking the GUI. Returns the result of 'task', or raises the same exception. This method blocks until 'task' is finished. diff --git a/electrumabc/bitcoin.py b/electrumabc/bitcoin.py index 1ea6e147ff3a..93fdaceb46db 100644 --- a/electrumabc/bitcoin.py +++ b/electrumabc/bitcoin.py @@ -1508,13 +1508,13 @@ def __init__(self, enc, *, net=None): ) @property - def lot(self) -> int: + def lot(self) -> Optional[int]: """Returns the 'lot' number if 'hasLotSequence' or None otherwise.""" if self.dec and self.hasLotSequence: return self.entropy[4] * 4096 + self.entropy[5] * 16 + self.entropy[6] // 16 @property - def sequence(self) -> int: + def sequence(self) -> Optional[int]: """Returns the 'sequence' number if 'hasLotSequence' or None otherwise.""" if self.dec and self.hasLotSequence: @@ -1783,7 +1783,7 @@ def encrypt(cls, wif: str, passphrase: str, *, net=None) -> object: def createECMult( cls, passphrase: str, - lot_sequence: Tuple[int, int] = None, + lot_sequence: Optional[Tuple[int, int]] = None, compressed=True, *, net=None, diff --git a/electrumabc/contacts.py b/electrumabc/contacts.py index 20c10dc8f230..40210ed038d2 100644 --- a/electrumabc/contacts.py +++ b/electrumabc/contacts.py @@ -26,7 +26,7 @@ import re import traceback from collections import namedtuple -from typing import List +from typing import List, Optional import dns from dns.exception import DNSException @@ -303,7 +303,10 @@ def replace(self, old: Contact, new: Contact): return False def add( - self, contact: Contact, replace_old: Contact = None, unique: bool = False + self, + contact: Contact, + replace_old: Optional[Contact] = None, + unique: bool = False, ) -> bool: """Puts a contact in the contact list, appending it at the end. Optionally, if replace_old is specified, will replace the entry diff --git a/electrumabc/plugins.py b/electrumabc/plugins.py index 6052375f811a..835ee366d7c3 100644 --- a/electrumabc/plugins.py +++ b/electrumabc/plugins.py @@ -1092,7 +1092,7 @@ def unpaired_device_infos( self, handler: Optional[HardwareHandlerBase], plugin: HW_PluginBase, - devices: List[Device] = None, + devices: Optional[List[Device]] = None, ) -> List["DeviceInfo"]: """Returns a list of DeviceInfo objects: one for each connected, unpaired device accepted by the plugin.""" @@ -1122,7 +1122,7 @@ def select_device( plugin: HW_PluginBase, handler: HardwareHandlerBase, keystore: Hardware_KeyStore, - devices: List["Device"] = None, + devices: Optional[List["Device"]] = None, ) -> DeviceInfo: """Ask the user to select a device to use if there is more than one, and return the DeviceInfo for the device.""" diff --git a/electrumabc/transaction.py b/electrumabc/transaction.py index 73fa97d52539..08d688e52110 100644 --- a/electrumabc/transaction.py +++ b/electrumabc/transaction.py @@ -26,7 +26,7 @@ import hashlib import struct import warnings -from typing import Tuple, Union +from typing import Optional, Tuple, Union import ecdsa @@ -1574,7 +1574,7 @@ def tx_cache_get(cls, txid: str) -> object: return None @classmethod - def tx_cache_put(cls, tx: object, txid: str = None): + def tx_cache_put(cls, tx: object, txid: Optional[str] = None): """Puts a non-deserialized copy of tx into the tx_cache.""" if not tx or not tx.raw: raise ValueError("Please pass a tx which has a valid .raw attribute!") diff --git a/electrumabc/wallet.py b/electrumabc/wallet.py index c7fadd40d3a7..8f64772393ba 100644 --- a/electrumabc/wallet.py +++ b/electrumabc/wallet.py @@ -2954,12 +2954,12 @@ def rebuild_history(self): self.start_threads(network) self.network.trigger_callback("wallet_updated", self) - def is_schnorr_possible(self, reason: list = None) -> bool: + def is_schnorr_possible(self, reason: Optional[list] = None) -> bool: """Returns True if this wallet type is compatible. `reason` is an optional list where you would like a translated string of why Schnorr isn't possible placed (on False return).""" ok = bool(not self.is_multisig() and not self.is_hardware()) - if not ok and isinstance(reason, list): + if not ok and reason is not None: reason.insert(0, _("Schnorr signatures are disabled for this wallet type.")) return ok diff --git a/electrumabc/winconsole.py b/electrumabc/winconsole.py index 1ca06ed3732d..981d44eb2a80 100644 --- a/electrumabc/winconsole.py +++ b/electrumabc/winconsole.py @@ -32,12 +32,13 @@ import ctypes import os import sys +from typing import Iterator, Optional STD_OUTPUT_HANDLE = -11 FILE_TYPE_DISK = 1 -def parent_process_pids() -> int: +def parent_process_pids() -> Iterator[int]: """ Returns all parent process PIDs, starting with the closest parent """ @@ -58,16 +59,15 @@ def get_console_title() -> str: b = bytes(1024) b_ptr = ctypes.c_char_p(b) title = None - title_len = ctypes.windll.kernel32.GetConsoleTitleW( - b_ptr, len(b) // 2 - ) # GetConsoleTitleW expects size in 2-byte chars + # GetConsoleTitleW expects size in 2-byte chars + title_len = ctypes.windll.kernel32.GetConsoleTitleW(b_ptr, len(b) // 2) # type: ignore[attr-defined] if title_len > 0: title = b.decode("utf-16")[:title_len] return title def create_or_attach_console( - *, attach: bool = True, create: bool = False, title: str = None + *, attach: bool = True, create: bool = False, title: Optional[str] = None ) -> bool: """ Workaround to the fact that cmd.exe based execution of this program means @@ -90,8 +90,7 @@ def create_or_attach_console( (sys.stdout, sys.stderr) to this found and/or created console. Always return True on success or if there was a console already, - False or None on failure (a None return indicates a missing lib or some - other unspecified exception was raised when attempting to create a console). + False on failure. """ std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) @@ -111,7 +110,7 @@ def create_or_attach_console( break except ImportError: # User's system lacks psutil - return # Return None in case caller wants to differntiate exceptional failures from regular False return + return False created = False @@ -134,12 +133,11 @@ def create_or_attach_console( except OSError: # If we get here, we likely were in MinGW / MSYS where CONOUT$ / CONIN$ # are not valid files or some other weirdness occurred. Give up. - return # return None to indicate underlying exception + return False if title: - old_title = ( - get_console_title() if not created else None - ) # save the old title only if not created by us + # save the old title only if not created by us + old_title = get_console_title() if not created else None # Set the console title, if specified ctypes.windll.kernel32.SetConsoleTitleW(title) if old_title is not None: diff --git a/electrumabc_gui/qt/avalanche/proof_editor.py b/electrumabc_gui/qt/avalanche/proof_editor.py index 1888891d6451..f7340e7a4507 100644 --- a/electrumabc_gui/qt/avalanche/proof_editor.py +++ b/electrumabc_gui/qt/avalanche/proof_editor.py @@ -79,7 +79,7 @@ def __init__( self, wallet: Deterministic_Wallet, receive_address: Optional[Address] = None, - parent: QtWidgets.QWidget = None, + parent: Optional[QtWidgets.QWidget] = None, ): CachedWalletPasswordWidget.__init__(self, wallet, parent=parent) # This is enough width to show a whole compressed pubkey. diff --git a/electrumabc_gui/qt/avalanche/util.py b/electrumabc_gui/qt/avalanche/util.py index d195472cf16e..5a1b92d36f79 100644 --- a/electrumabc_gui/qt/avalanche/util.py +++ b/electrumabc_gui/qt/avalanche/util.py @@ -42,7 +42,7 @@ def __init__( self, wallet: Deterministic_Wallet, pwd: Optional[str] = None, - parent: QtWidgets.QWidget = None, + parent: Optional[QtWidgets.QWidget] = None, ): super().__init__(parent) self._pwd = pwd @@ -168,7 +168,7 @@ def __init__( self, wallet: Deterministic_Wallet, pwd: Optional[str] = None, - parent: QtWidgets.QWidget = None, + parent: Optional[QtWidgets.QWidget] = None, additional_info: Optional[str] = None, ): super().__init__(parent) diff --git a/electrumabc_gui/qt/main_window.py b/electrumabc_gui/qt/main_window.py index bff31e3556b0..7e5181bfda10 100644 --- a/electrumabc_gui/qt/main_window.py +++ b/electrumabc_gui/qt/main_window.py @@ -3447,7 +3447,7 @@ def change_password_dialog(self): self.status_bar.update_lock_icon(self.wallet.has_password()) def get_passphrase_dialog( - self, msg: str, title: str = None, *, permit_empty=False + self, msg: str, title: Optional[str] = None, *, permit_empty=False ) -> str: d = PassphraseDialog( self.wallet, self.top_level_window(), msg, title, permit_empty=permit_empty diff --git a/electrumabc_gui/qt/qrreader/crop_blur_effect.py b/electrumabc_gui/qt/qrreader/crop_blur_effect.py index 6c49b29ff7e0..ccf438a9743f 100644 --- a/electrumabc_gui/qt/qrreader/crop_blur_effect.py +++ b/electrumabc_gui/qt/qrreader/crop_blur_effect.py @@ -23,6 +23,7 @@ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from typing import Optional from PyQt5 import QtWidgets from PyQt5.QtCore import QObject, QPoint, QRect, Qt @@ -36,12 +37,12 @@ class QrReaderCropBlurEffect(QtWidgets.QGraphicsBlurEffect): BLUR_DARKEN = 0.25 BLUR_RADIUS = 8 - def __init__(self, parent: QObject, crop: QRect = None): + def __init__(self, parent: QObject, crop: Optional[QRect] = None): super().__init__(parent) self.crop = crop self.setBlurRadius(self.BLUR_RADIUS) - def setCrop(self, crop: QRect = None): + def setCrop(self, crop: Optional[QRect] = None): self.crop = crop def draw(self, painter: QPainter): diff --git a/electrumabc_gui/qt/qrreader/video_overlay.py b/electrumabc_gui/qt/qrreader/video_overlay.py index 9b72c1a9f838..27bf2778fae4 100644 --- a/electrumabc_gui/qt/qrreader/video_overlay.py +++ b/electrumabc_gui/qt/qrreader/video_overlay.py @@ -24,7 +24,7 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -from typing import List +from typing import List, Optional from PyQt5 import QtWidgets from PyQt5.QtCore import QPoint, QRect, QRectF, QSize, Qt @@ -48,7 +48,7 @@ class QrReaderVideoOverlay(QtWidgets.QWidget): QR_FINDER_OPACITY = 0.25 QR_FINDER_SIZE = 0.5 - def __init__(self, parent: QtWidgets.QWidget = None): + def __init__(self, parent: Optional[QtWidgets.QWidget] = None): super().__init__(parent) self.results = [] diff --git a/electrumabc_gui/qt/qrreader/video_surface.py b/electrumabc_gui/qt/qrreader/video_surface.py index 478234157509..b83765e2b8c1 100644 --- a/electrumabc_gui/qt/qrreader/video_surface.py +++ b/electrumabc_gui/qt/qrreader/video_surface.py @@ -24,7 +24,7 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -from typing import List +from typing import List, Optional from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtGui import QImage @@ -45,7 +45,7 @@ class QrReaderVideoSurface(QAbstractVideoSurface): necessary and sends them to listeners via the frame_available event. """ - def __init__(self, parent: QObject = None): + def __init__(self, parent: Optional[QObject] = None): super().__init__(parent) def present(self, frame: QVideoFrame) -> bool: diff --git a/electrumabc_gui/qt/qrreader/video_widget.py b/electrumabc_gui/qt/qrreader/video_widget.py index a013efeba8a2..0c9895e97d6d 100644 --- a/electrumabc_gui/qt/qrreader/video_widget.py +++ b/electrumabc_gui/qt/qrreader/video_widget.py @@ -23,6 +23,7 @@ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from typing import Optional from PyQt5 import QtWidgets from PyQt5.QtGui import QPainter, QPaintEvent, QPixmap @@ -35,7 +36,7 @@ class QrReaderVideoWidget(QtWidgets.QWidget): USE_BILINEAR_FILTER = True - def __init__(self, parent: QtWidgets.QWidget = None): + def __init__(self, parent: Optional[QtWidgets.QWidget] = None): super().__init__(parent) self.pixmap = None diff --git a/electrumabc_gui/qt/sign_verify_dialog.py b/electrumabc_gui/qt/sign_verify_dialog.py index b9eb37834d5b..b3902ab6ff8b 100644 --- a/electrumabc_gui/qt/sign_verify_dialog.py +++ b/electrumabc_gui/qt/sign_verify_dialog.py @@ -33,7 +33,9 @@ class CollapsibleSection(QWidget): - def __init__(self, title: str, content_widget: QWidget, parent: QWidget = None): + def __init__( + self, title: str, content_widget: QWidget, parent: Optional[QWidget] = None + ): super().__init__(parent) main_layout = QGridLayout(self) diff --git a/electrumabc_gui/qt/style_patcher.py b/electrumabc_gui/qt/style_patcher.py index 8ca8a7527097..0c0bd2a54e6c 100644 --- a/electrumabc_gui/qt/style_patcher.py +++ b/electrumabc_gui/qt/style_patcher.py @@ -2,6 +2,7 @@ This is used to patch the QApplication style sheet. It reads the current stylesheet, appends our modifications and sets the new stylesheet. """ +from typing import Optional from PyQt5 import QtWidgets @@ -37,7 +38,7 @@ """ -def patch(use_dark_theme: bool = False, darkstyle_ver: tuple = None): +def patch(use_dark_theme: bool = False, darkstyle_ver: Optional[tuple] = None): if not use_dark_theme: return custom_patch = "" diff --git a/electrumabc_gui/qt/util.py b/electrumabc_gui/qt/util.py index 11e3b7a4f2e1..10ddffb5c964 100644 --- a/electrumabc_gui/qt/util.py +++ b/electrumabc_gui/qt/util.py @@ -9,6 +9,7 @@ from collections import namedtuple from functools import partial, wraps from locale import atof +from typing import Optional from PyQt5 import QtWidgets from PyQt5.QtCore import QObject, Qt, QThread, QTimer, pyqtSignal @@ -980,7 +981,7 @@ def _updateOverlayPos(self): x -= scrollbar_width self.overlay_widget.move(x, y) - def addWidget(self, widget: QtWidgets.QWidget, index: int = None): + def addWidget(self, widget: QtWidgets.QWidget, index: Optional[int] = None): if index is not None: self.overlay_layout.insertWidget(index, widget) else: @@ -991,9 +992,9 @@ def addButton( icon_name: str, on_click, tooltip: str, - index: int = None, + index: Optional[int] = None, *, - text: str = None, + text: Optional[str] = None, ) -> QtWidgets.QAbstractButton: """icon_name may be None but then you must define text (which is hopefully then some nice Unicode character). Both cannot be None. @@ -1540,7 +1541,7 @@ class TextBrowserKeyboardFocusFilter(QtWidgets.QTextBrowser): deactivate keyboard text selection. """ - def __init__(self, parent: QtWidgets.QWidget = None): + def __init__(self, parent: Optional[QtWidgets.QWidget] = None): super().__init__(parent) def focusInEvent(self, e: QFocusEvent): diff --git a/electrumabc_gui/qt/utils/aspect_layout.py b/electrumabc_gui/qt/utils/aspect_layout.py index d610fa8b0c8e..609b2dd28a7a 100644 --- a/electrumabc_gui/qt/utils/aspect_layout.py +++ b/electrumabc_gui/qt/utils/aspect_layout.py @@ -24,14 +24,16 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -from typing import List +from typing import List, Optional from PyQt5 import QtWidgets from PyQt5.QtCore import QPoint, QRect, QSize, Qt class FixedAspectRatioLayout(QtWidgets.QLayout): - def __init__(self, parent: QtWidgets.QWidget = None, aspect_ratio: float = 1.0): + def __init__( + self, parent: Optional[QtWidgets.QWidget] = None, aspect_ratio: float = 1.0 + ): super().__init__(parent) self.aspect_ratio = aspect_ratio self.items: List[QtWidgets.QLayoutItem] = [] diff --git a/electrumabc_gui/qt/utils/aspect_svg_widget.py b/electrumabc_gui/qt/utils/aspect_svg_widget.py index 62fa3428c6b5..cf1946f0357c 100644 --- a/electrumabc_gui/qt/utils/aspect_svg_widget.py +++ b/electrumabc_gui/qt/utils/aspect_svg_widget.py @@ -23,13 +23,16 @@ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from typing import Optional from PyQt5.QtCore import QObject, QSize from PyQt5.QtSvg import QSvgWidget class FixedAspectRatioSvgWidget(QSvgWidget): - def __init__(self, width: int, file: str = None, parent: QObject = None): + def __init__( + self, width: int, file: Optional[str] = None, parent: Optional[QObject] = None + ): if file: super().__init__(file, parent) else: diff --git a/electrumabc_plugins/fusion/plugin.py b/electrumabc_plugins/fusion/plugin.py index 7ce47818f76e..33d3df45a534 100644 --- a/electrumabc_plugins/fusion/plugin.py +++ b/electrumabc_plugins/fusion/plugin.py @@ -761,7 +761,7 @@ def update_coins_ui(self, wallet): This is called by the Fusion thread (in its thread context) when it freezes & unfreezes coins.""" - def notify_server_status(self, b, tup: tuple = None): + def notify_server_status(self, b, tup: Optional[tuple] = None): """The Qt plugin subclass implements this to tell the GUI about bad servers.""" if not b: