mirror of
https://github.com/certbot/certbot.git
synced 2026-07-31 18:34:41 +02:00
Prepare certbot module for mypy check untyped defs (#6005)
* Prepare certbot module for mypy check untyped defs * Fix #5952 * Bump mypy to version 0.600 and fix associated bugs * Fix pylint bugs after introducing mypy * Implement Brad's suggestions * Reenabling pylint and adding nginx mypy back
This commit is contained in:
committed by
Brad Warren
parent
250c0d6691
commit
36dfd06503
@@ -10,6 +10,7 @@ import zope.component
|
||||
from acme import challenges
|
||||
from acme import client as acme_client
|
||||
from acme import messages
|
||||
from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module
|
||||
|
||||
from certbot import achallenges
|
||||
from certbot import errors
|
||||
@@ -354,12 +355,13 @@ class PollChallengesTest(unittest.TestCase):
|
||||
acme_util.CHALLENGES, [messages.STATUS_PENDING] * 3, False), [])
|
||||
]
|
||||
|
||||
self.chall_update = {}
|
||||
self.chall_update = {} # type: Dict[int, achallenges.KeyAuthorizationAnnotatedChallenge]
|
||||
for i, aauthzr in enumerate(self.aauthzrs):
|
||||
self.chall_update[i] = [
|
||||
challb_to_achall(challb, mock.Mock(key="dummy_key"), self.doms[i])
|
||||
for challb in aauthzr.authzr.body.challenges]
|
||||
|
||||
|
||||
@mock.patch("certbot.auth_handler.time")
|
||||
def test_poll_challenges(self, unused_mock_time):
|
||||
self.mock_net.poll.side_effect = self._mock_poll_solve_one_valid
|
||||
|
||||
@@ -495,7 +495,8 @@ class SetByCliTest(unittest.TestCase):
|
||||
for v in ('manual', 'manual_auth_hook', 'manual_public_ip_logging_ok'):
|
||||
self.assertTrue(_call_set_by_cli(v, args, verb))
|
||||
|
||||
cli.set_by_cli.detector = None
|
||||
# https://github.com/python/mypy/issues/2087
|
||||
cli.set_by_cli.detector = None # type: ignore
|
||||
|
||||
args = ['--manual-auth-hook', 'command']
|
||||
for v in ('manual_auth_hook', 'manual_public_ip_logging_ok'):
|
||||
|
||||
@@ -8,6 +8,7 @@ import unittest
|
||||
import mock
|
||||
from six.moves import reload_module # pylint: disable=import-error
|
||||
|
||||
from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module
|
||||
from certbot.tests.util import TempDirTestCase
|
||||
|
||||
class CompleterTest(TempDirTestCase):
|
||||
@@ -21,7 +22,7 @@ class CompleterTest(TempDirTestCase):
|
||||
if self.tempdir[-1] != os.sep:
|
||||
self.tempdir += os.sep
|
||||
|
||||
self.paths = []
|
||||
self.paths = [] # type: List[str]
|
||||
# create some files and directories in temp_dir
|
||||
for c in string.ascii_lowercase:
|
||||
path = os.path.join(self.tempdir, c)
|
||||
|
||||
@@ -6,6 +6,9 @@ import sys
|
||||
import unittest
|
||||
|
||||
import mock
|
||||
# pylint: disable=unused-import, no-name-in-module
|
||||
from acme.magic_typing import Callable, Dict, Union
|
||||
# pylint: enable=unused-import, no-name-in-module
|
||||
|
||||
|
||||
def get_signals(signums):
|
||||
@@ -23,8 +26,7 @@ def set_signals(sig_handler_dict):
|
||||
def signal_receiver(signums):
|
||||
"""Context manager to catch signals"""
|
||||
signals = []
|
||||
prev_handlers = {}
|
||||
prev_handlers = get_signals(signums)
|
||||
prev_handlers = get_signals(signums) # type: Dict[int, Union[int, None, Callable]]
|
||||
set_signals(dict((s, lambda s, _: signals.append(s)) for s in signums))
|
||||
yield signals
|
||||
set_signals(prev_handlers)
|
||||
|
||||
+11
-10
@@ -5,6 +5,7 @@ import unittest
|
||||
|
||||
import mock
|
||||
|
||||
from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module
|
||||
from certbot import errors
|
||||
from certbot.tests import util
|
||||
|
||||
@@ -106,8 +107,8 @@ class PreHookTest(HookTest):
|
||||
super(PreHookTest, self).tearDown()
|
||||
|
||||
def _reset_pre_hook_already(self):
|
||||
from certbot.hooks import pre_hook
|
||||
pre_hook.already.clear()
|
||||
from certbot.hooks import executed_pre_hooks
|
||||
executed_pre_hooks.clear()
|
||||
|
||||
def test_certonly(self):
|
||||
self.config.verb = "certonly"
|
||||
@@ -184,8 +185,8 @@ class PostHookTest(HookTest):
|
||||
super(PostHookTest, self).tearDown()
|
||||
|
||||
def _reset_post_hook_eventually(self):
|
||||
from certbot.hooks import post_hook
|
||||
post_hook.eventually = []
|
||||
from certbot.hooks import post_hooks
|
||||
del post_hooks[:]
|
||||
|
||||
def test_certonly_and_run_with_hook(self):
|
||||
for verb in ("certonly", "run",):
|
||||
@@ -238,8 +239,8 @@ class PostHookTest(HookTest):
|
||||
self.assertEqual(self._get_eventually(), expected)
|
||||
|
||||
def _get_eventually(self):
|
||||
from certbot.hooks import post_hook
|
||||
return post_hook.eventually
|
||||
from certbot.hooks import post_hooks
|
||||
return post_hooks
|
||||
|
||||
|
||||
class RunSavedPostHooksTest(HookTest):
|
||||
@@ -248,23 +249,23 @@ class RunSavedPostHooksTest(HookTest):
|
||||
@classmethod
|
||||
def _call(cls, *args, **kwargs):
|
||||
from certbot.hooks import run_saved_post_hooks
|
||||
return run_saved_post_hooks(*args, **kwargs)
|
||||
return run_saved_post_hooks()
|
||||
|
||||
def _call_with_mock_execute_and_eventually(self, *args, **kwargs):
|
||||
"""Call run_saved_post_hooks but mock out execute and eventually
|
||||
|
||||
certbot.hooks.post_hook.eventually is replaced with
|
||||
certbot.hooks.post_hooks is replaced with
|
||||
self.eventually. The mock execute object is returned rather than
|
||||
the return value of run_saved_post_hooks.
|
||||
|
||||
"""
|
||||
eventually_path = "certbot.hooks.post_hook.eventually"
|
||||
eventually_path = "certbot.hooks.post_hooks"
|
||||
with mock.patch(eventually_path, new=self.eventually):
|
||||
return self._call_with_mock_execute(*args, **kwargs)
|
||||
|
||||
def setUp(self):
|
||||
super(RunSavedPostHooksTest, self).setUp()
|
||||
self.eventually = []
|
||||
self.eventually = [] # type: List[str]
|
||||
|
||||
def test_empty(self):
|
||||
self.assertFalse(self._call_with_mock_execute_and_eventually().called)
|
||||
|
||||
@@ -10,6 +10,7 @@ import mock
|
||||
import six
|
||||
|
||||
from acme import messages
|
||||
from acme.magic_typing import Optional # pylint: disable=unused-import, no-name-in-module
|
||||
|
||||
from certbot import constants
|
||||
from certbot import errors
|
||||
@@ -21,9 +22,9 @@ class PreArgParseSetupTest(unittest.TestCase):
|
||||
"""Tests for certbot.log.pre_arg_parse_setup."""
|
||||
|
||||
@classmethod
|
||||
def _call(cls, *args, **kwargs):
|
||||
def _call(cls, *args, **kwargs): # pylint: disable=unused-argument
|
||||
from certbot.log import pre_arg_parse_setup
|
||||
return pre_arg_parse_setup(*args, **kwargs)
|
||||
return pre_arg_parse_setup()
|
||||
|
||||
@mock.patch('certbot.log.sys')
|
||||
@mock.patch('certbot.log.pre_arg_parse_except_hook')
|
||||
@@ -38,16 +39,16 @@ class PreArgParseSetupTest(unittest.TestCase):
|
||||
mock_root_logger.setLevel.assert_called_once_with(logging.DEBUG)
|
||||
self.assertEqual(mock_root_logger.addHandler.call_count, 2)
|
||||
|
||||
MemoryHandler = logging.handlers.MemoryHandler
|
||||
memory_handler = None
|
||||
memory_handler = None # type: Optional[logging.handlers.MemoryHandler]
|
||||
for call in mock_root_logger.addHandler.call_args_list:
|
||||
handler = call[0][0]
|
||||
if memory_handler is None and isinstance(handler, MemoryHandler):
|
||||
if memory_handler is None and isinstance(handler, logging.handlers.MemoryHandler):
|
||||
memory_handler = handler
|
||||
target = memory_handler.target # type: ignore
|
||||
else:
|
||||
self.assertTrue(isinstance(handler, logging.StreamHandler))
|
||||
self.assertTrue(
|
||||
isinstance(memory_handler.target, logging.StreamHandler))
|
||||
isinstance(target, logging.StreamHandler))
|
||||
|
||||
mock_register.assert_called_once_with(logging.shutdown)
|
||||
mock_sys.excepthook(1, 2, 3)
|
||||
|
||||
@@ -16,12 +16,14 @@ import josepy as jose
|
||||
import six
|
||||
from six.moves import reload_module # pylint: disable=import-error
|
||||
|
||||
from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module
|
||||
from certbot import account
|
||||
from certbot import cli
|
||||
from certbot import constants
|
||||
from certbot import configuration
|
||||
from certbot import crypto_util
|
||||
from certbot import errors
|
||||
from certbot import interfaces # pylint: disable=unused-import
|
||||
from certbot import main
|
||||
from certbot import updater
|
||||
from certbot import util
|
||||
@@ -600,14 +602,14 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met
|
||||
|
||||
if mockisfile:
|
||||
orig_open = os.path.isfile
|
||||
def mock_isfile(fn, *args, **kwargs):
|
||||
def mock_isfile(fn, *args, **kwargs): # pylint: disable=unused-argument
|
||||
"""Mock os.path.isfile()"""
|
||||
if (fn.endswith("cert") or
|
||||
fn.endswith("chain") or
|
||||
fn.endswith("privkey")):
|
||||
return True
|
||||
else:
|
||||
return orig_open(fn, *args, **kwargs)
|
||||
return orig_open(fn)
|
||||
|
||||
with mock.patch("os.path.isfile") as mock_if:
|
||||
mock_if.side_effect = mock_isfile
|
||||
@@ -836,7 +838,7 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met
|
||||
@mock.patch('certbot.main.plugins_disco')
|
||||
@mock.patch('certbot.main.cli.HelpfulArgumentParser.determine_help_topics')
|
||||
def test_plugins_no_args(self, _det, mock_disco):
|
||||
ifaces = []
|
||||
ifaces = [] # type: List[interfaces.IPlugin]
|
||||
plugins = mock_disco.PluginsRegistry.find_all()
|
||||
|
||||
stdout = six.StringIO()
|
||||
@@ -851,7 +853,7 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met
|
||||
@mock.patch('certbot.main.plugins_disco')
|
||||
@mock.patch('certbot.main.cli.HelpfulArgumentParser.determine_help_topics')
|
||||
def test_plugins_no_args_unprivileged(self, _det, mock_disco):
|
||||
ifaces = []
|
||||
ifaces = [] # type: List[interfaces.IPlugin]
|
||||
plugins = mock_disco.PluginsRegistry.find_all()
|
||||
|
||||
def throw_error(directory, mode, uid, strict):
|
||||
@@ -873,7 +875,7 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met
|
||||
@mock.patch('certbot.main.plugins_disco')
|
||||
@mock.patch('certbot.main.cli.HelpfulArgumentParser.determine_help_topics')
|
||||
def test_plugins_init(self, _det, mock_disco):
|
||||
ifaces = []
|
||||
ifaces = [] # type: List[interfaces.IPlugin]
|
||||
plugins = mock_disco.PluginsRegistry.find_all()
|
||||
|
||||
stdout = six.StringIO()
|
||||
@@ -891,7 +893,7 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met
|
||||
@mock.patch('certbot.main.plugins_disco')
|
||||
@mock.patch('certbot.main.cli.HelpfulArgumentParser.determine_help_topics')
|
||||
def test_plugins_prepare(self, _det, mock_disco):
|
||||
ifaces = []
|
||||
ifaces = [] # type: List[interfaces.IPlugin]
|
||||
plugins = mock_disco.PluginsRegistry.find_all()
|
||||
|
||||
stdout = six.StringIO()
|
||||
@@ -1040,9 +1042,8 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met
|
||||
mock_client.obtain_certificate.return_value = (mock_certr, 'chain',
|
||||
mock_key, 'csr')
|
||||
|
||||
def write_msg(message, *args, **kwargs):
|
||||
def write_msg(message, *args, **kwargs): # pylint: disable=unused-argument
|
||||
"""Write message to stdout."""
|
||||
_, _ = args, kwargs
|
||||
stdout.write(message)
|
||||
|
||||
try:
|
||||
|
||||
@@ -12,7 +12,7 @@ class ReporterTest(unittest.TestCase):
|
||||
from certbot import reporter
|
||||
self.reporter = reporter.Reporter(mock.MagicMock(quiet=False))
|
||||
|
||||
self.old_stdout = sys.stdout
|
||||
self.old_stdout = sys.stdout # type: ignore
|
||||
sys.stdout = six.StringIO()
|
||||
|
||||
def tearDown(self):
|
||||
@@ -21,32 +21,32 @@ class ReporterTest(unittest.TestCase):
|
||||
def test_multiline_message(self):
|
||||
self.reporter.add_message("Line 1\nLine 2", self.reporter.LOW_PRIORITY)
|
||||
self.reporter.print_messages()
|
||||
output = sys.stdout.getvalue()
|
||||
output = sys.stdout.getvalue() # type: ignore
|
||||
self.assertTrue("Line 1\n" in output)
|
||||
self.assertTrue("Line 2" in output)
|
||||
|
||||
def test_tty_print_empty(self):
|
||||
sys.stdout.isatty = lambda: True
|
||||
sys.stdout.isatty = lambda: True # type: ignore
|
||||
self.test_no_tty_print_empty()
|
||||
|
||||
def test_no_tty_print_empty(self):
|
||||
self.reporter.print_messages()
|
||||
self.assertEqual(sys.stdout.getvalue(), "")
|
||||
self.assertEqual(sys.stdout.getvalue(), "") # type: ignore
|
||||
try:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
self.reporter.print_messages()
|
||||
self.assertEqual(sys.stdout.getvalue(), "")
|
||||
self.assertEqual(sys.stdout.getvalue(), "") # type: ignore
|
||||
|
||||
def test_tty_successful_exit(self):
|
||||
sys.stdout.isatty = lambda: True
|
||||
sys.stdout.isatty = lambda: True # type: ignore
|
||||
self._successful_exit_common()
|
||||
|
||||
def test_no_tty_successful_exit(self):
|
||||
self._successful_exit_common()
|
||||
|
||||
def test_tty_unsuccessful_exit(self):
|
||||
sys.stdout.isatty = lambda: True
|
||||
sys.stdout.isatty = lambda: True # type: ignore
|
||||
self._unsuccessful_exit_common()
|
||||
|
||||
def test_no_tty_unsuccessful_exit(self):
|
||||
@@ -55,7 +55,7 @@ class ReporterTest(unittest.TestCase):
|
||||
def _successful_exit_common(self):
|
||||
self._add_messages()
|
||||
self.reporter.print_messages()
|
||||
output = sys.stdout.getvalue()
|
||||
output = sys.stdout.getvalue() # type: ignore
|
||||
self.assertTrue("IMPORTANT NOTES:" in output)
|
||||
self.assertTrue("High" in output)
|
||||
self.assertTrue("Med" in output)
|
||||
@@ -67,7 +67,7 @@ class ReporterTest(unittest.TestCase):
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
self.reporter.print_messages()
|
||||
output = sys.stdout.getvalue()
|
||||
output = sys.stdout.getvalue() # type: ignore
|
||||
self.assertTrue("IMPORTANT NOTES:" in output)
|
||||
self.assertTrue("High" in output)
|
||||
self.assertTrue("Med" not in output)
|
||||
|
||||
Reference in New Issue
Block a user