From 608d731e2b1cbb621cfe6d13ed0c2e72ec3a2de5 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Thu, 30 Mar 2023 15:20:44 -0700 Subject: [PATCH] Make mypy pass on our tests (#9648) * make mypy pass on our tests * fix grammar --- .../apache-conf-files/apache-conf-test-pebble.py | 5 ++--- certbot-nginx/certbot_nginx/_internal/parser_obj.py | 3 ++- .../certbot_nginx/_internal/tests/http_01_test.py | 13 +++++++------ .../_internal/tests/parser_obj_test.py | 3 +++ .../certbot/_internal/tests/plugins/common_test.py | 5 +++-- certbot/certbot/_internal/tests/util_test.py | 11 ++--------- mypy.ini | 12 +++++++++--- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/certbot-apache/certbot_apache/_internal/tests/apache-conf-files/apache-conf-test-pebble.py b/certbot-apache/certbot_apache/_internal/tests/apache-conf-files/apache-conf-test-pebble.py index 68bd6287d..383c652f8 100755 --- a/certbot-apache/certbot_apache/_internal/tests/apache-conf-files/apache-conf-test-pebble.py +++ b/certbot-apache/certbot_apache/_internal/tests/apache-conf-files/apache-conf-test-pebble.py @@ -12,9 +12,8 @@ from certbot_integration_tests.utils import acme_server SCRIPT_DIRNAME = os.path.dirname(__file__) -def main(args=None): - if not args: - args = sys.argv[1:] +def main() -> int: + args = sys.argv[1:] with acme_server.ACMEServer('pebble', [], False) as acme_xdist: environ = os.environ.copy() environ['SERVER'] = acme_xdist['directory_url'] diff --git a/certbot-nginx/certbot_nginx/_internal/parser_obj.py b/certbot-nginx/certbot_nginx/_internal/parser_obj.py index 0af38a936..7a0946500 100644 --- a/certbot-nginx/certbot_nginx/_internal/parser_obj.py +++ b/certbot-nginx/certbot_nginx/_internal/parser_obj.py @@ -1,5 +1,6 @@ # type: ignore -# This module is not used for now, so we just skip type check for the sake of simplicity. +# This module is not used for now, so we just skip type checking for the sake +# of simplicity. """ This file contains parsing routines and object classes to help derive meaning from raw lists of tokens from pyparsing. """ diff --git a/certbot-nginx/certbot_nginx/_internal/tests/http_01_test.py b/certbot-nginx/certbot_nginx/_internal/tests/http_01_test.py index 6726b85ad..c81357607 100644 --- a/certbot-nginx/certbot_nginx/_internal/tests/http_01_test.py +++ b/certbot-nginx/certbot_nginx/_internal/tests/http_01_test.py @@ -7,6 +7,7 @@ import josepy as jose import pytest from acme import challenges +from acme import messages from certbot import achallenges from certbot.tests import acme_util from certbot.tests import util as test_util @@ -23,29 +24,29 @@ class HttpPerformTest(util.NginxTest): achalls = [ achallenges.KeyAuthorizationAnnotatedChallenge( challb=acme_util.chall_to_challb( - challenges.HTTP01(token=b"kNdwjwOeX0I_A8DXt9Msmg"), "pending"), + challenges.HTTP01(token=b"kNdwjwOeX0I_A8DXt9Msmg"), messages.STATUS_PENDING), domain="www.example.com", account_key=account_key), achallenges.KeyAuthorizationAnnotatedChallenge( challb=acme_util.chall_to_challb( challenges.HTTP01( token=b"\xba\xa9\xda? str: - pass + return "info" @classmethod def add_parser_arguments(cls, add): diff --git a/certbot/certbot/_internal/tests/util_test.py b/certbot/certbot/_internal/tests/util_test.py index ac3b5ab88..b4256176e 100644 --- a/certbot/certbot/_internal/tests/util_test.py +++ b/certbot/certbot/_internal/tests/util_test.py @@ -247,13 +247,6 @@ class UniqueFileTest(test_util.TempDirTestCase): fd3.close() -try: - file_type = file -except NameError: - import io - file_type = io.TextIOWrapper # type: ignore - - class UniqueLineageNameTest(test_util.TempDirTestCase): """Tests for certbot.util.unique_lineage_name.""" @@ -263,7 +256,7 @@ class UniqueLineageNameTest(test_util.TempDirTestCase): def test_basic(self): f, path = self._call("wow") - assert isinstance(f, file_type) + assert isinstance(f, io.TextIOWrapper) assert os.path.join(self.tempdir, "wow.conf") == path f.close() @@ -272,7 +265,7 @@ class UniqueLineageNameTest(test_util.TempDirTestCase): for _ in range(10): items.append(self._call("wow")) f, name = items[-1] - assert isinstance(f, file_type) + assert isinstance(f, io.TextIOWrapper) assert isinstance(name, str) assert "wow-0009.conf" in name for f, _ in items: diff --git a/mypy.ini b/mypy.ini index 6c01929d4..2c9ba5187 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,8 +1,14 @@ [mypy] -# Removing this exclude setting is being tracked by -# https://github.com/certbot/certbot/issues/7909. -exclude = .*/_internal/tests/ ignore_missing_imports = True warn_unused_ignores = True show_error_codes = True disallow_untyped_defs = True + +# Using stricter settings here is being tracked by +# https://github.com/certbot/certbot/issues/9647. +[mypy-*._internal.tests.*] +# By default, mypy prints notes without erroring about any type annotations it +# finds in untyped function bodies when check_untyped_defs is false. Disabling +# this "error" code removes this visual noise. +disable_error_code = annotation-unchecked +disallow_untyped_defs = False