Skip to content

Commit

Permalink
type checking: resolve all current type checking issues
Browse files Browse the repository at this point in the history
* ignore missing imports

https://mypy.readthedocs.io/en/latest/running_mypy.html#ignore-missing-imports

* mypy doesn't handle dynamic base classes

see workaround in python/mypy#2477

* rework language_code handling to resolve TypeError

mypy was flagging this logic due to the fact that current_locale
is Optional[str]

* TimedRotatingFileHandler delay arg should be bool
  • Loading branch information
redshiftzero committed Apr 26, 2019
1 parent 5c36c07 commit b266505
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SHELL := /bin/bash

.PHONY: mypy
mypy: ## Run static type checker
@mypy securedrop_client
@mypy --ignore-missing-imports securedrop_client

.PHONY: clean
clean: ## Clean the workspace of generated resources
Expand Down
7 changes: 5 additions & 2 deletions securedrop_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
# Use the operating system's locale.
current_locale, encoding = locale.getdefaultlocale()
# Get the language code.
language_code = current_locale[:2]
except (TypeError, ValueError): # pragma: no cover
if current_locale is None:
language_code = 'en'
else:
language_code = current_locale[:2]
except ValueError: # pragma: no cover
language_code = 'en' # pragma: no cover
# DEBUG/TRANSLATE: override the language code here (e.g. to Chinese).
# language_code = 'zh'
Expand Down
2 changes: 1 addition & 1 deletion securedrop_client/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def configure_logging(sdc_home: str) -> None:

# define log handlers such as for rotating log files
handler = TimedRotatingFileHandler(log_file, when='midnight',
backupCount=5, delay=0,
backupCount=5, delay=False,
encoding=ENCODING)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
Expand Down
4 changes: 3 additions & 1 deletion securedrop_client/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from typing import Any # noqa: F401

from sqlalchemy import Boolean, Column, create_engine, DateTime, ForeignKey, Integer, String, \
Text, MetaData, CheckConstraint, text, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
Expand All @@ -16,7 +18,7 @@

metadata = MetaData(naming_convention=convention)

Base = declarative_base(metadata=metadata)
Base = declarative_base(metadata=metadata) # type: Any


def make_engine(home: str):
Expand Down
6 changes: 4 additions & 2 deletions securedrop_client/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import logging
from typing import List
from gettext import gettext as _
from typing import Dict, List, Optional # noqa: F401

from PyQt5.QtWidgets import QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QDesktopWidget, \
QApplication

from securedrop_client import __version__
from securedrop_client.db import Source
from securedrop_client.storage import source_exists
from securedrop_client.logic import Controller # noqa: F401
from securedrop_client.gui.widgets import TopPane, LeftPane, MainView, LoginDialog, \
SourceConversationWrapper
from securedrop_client.resources import load_icon
Expand Down Expand Up @@ -57,7 +59,7 @@ def __init__(self, sdc_home: str):
self.sdc_home = sdc_home
# Cache a dict of source.uuid -> SourceConversationWrapper
# We do this to not create/destroy widgets constantly (because it causes UI "flicker")
self.conversations = {}
self.conversations = {} # type: Dict

self.setWindowTitle(_("SecureDrop Controller {}").format(__version__))
self.setWindowIcon(load_icon(self.icon))
Expand Down
17 changes: 8 additions & 9 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
import logging
import arrow
from gettext import gettext as _
import html
import sys
from typing import List
Expand Down Expand Up @@ -727,15 +728,13 @@ def _construct_message(self, source: Source) -> str:
elif isinstance(submission, File):
files += 1

message = (
"<big>Deleting the Source account for",
"<b>{}</b> will also".format(source.journalist_designation,),
"delete {} files, {} replies, and {} messages.</big>".format(files, replies, messages),
"<br>",
"<small>This Source will no longer be able to correspond",
"through the log-in tied to this account.</small>",
)
message = ' '.join(message)
message = ("<big>Deleting the Source account for "
"<b>{}</b> will also "
"delete {} files, {} replies, and {} messages.</big>"
" <br> "
"<small>This Source will no longer be able to correspond "
"through the log-in tied to this account.</small>").format(
source.journalist_designation, files, replies, messages)
return message


Expand Down
9 changes: 5 additions & 4 deletions securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import uuid

from PyQt5.QtCore import QObject, QThread, pyqtSignal, QTimer, QProcess
from typing import Dict, Tuple # noqa: F401

from securedrop_client import storage
from securedrop_client import db
Expand Down Expand Up @@ -143,9 +144,9 @@ def __init__(self, hostname, gui, session,
self.gui = gui

# Reference to the API for secure drop proxy.
self.api = None
self.api = None # type: sdclientapi.API
# Contains active threads calling the API.
self.api_threads = {}
self.api_threads = {} # type: Dict[str, Dict]

# Reference to the SqlAlchemy session.
self.session = session
Expand Down Expand Up @@ -719,7 +720,7 @@ def send_reply(self, source_uuid: str, msg_uuid: str, message: str) -> None:
logger.error('not logged in - not implemented!') # pragma: no cover
self.reply_failed.emit(msg_uuid) # pragma: no cover

def _on_reply_complete(self, result, current_object: (str, str)) -> None:
def _on_reply_complete(self, result, current_object: Tuple[str, str]) -> None:
source_uuid, reply_uuid = current_object
source = self.session.query(db.Source).filter_by(uuid=source_uuid).one()
if isinstance(result, sdclientapi.Reply):
Expand All @@ -735,6 +736,6 @@ def _on_reply_complete(self, result, current_object: (str, str)) -> None:
else:
self.reply_failed.emit(reply_uuid)

def _on_reply_timeout(self, current_object: (str, str)) -> None:
def _on_reply_timeout(self, current_object: Tuple[str, str]) -> None:
_, reply_uuid = current_object
self.reply_failed.emit(reply_uuid)

0 comments on commit b266505

Please sign in to comment.