From e8cc2df31696127619e578fab8f22ba210515441 Mon Sep 17 00:00:00 2001 From: ohemorange Date: Tue, 28 Jan 2025 16:42:08 -0800 Subject: [PATCH] [nginx] Add type hints to nginx configurator for mypy --strict (#10156) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Decided that imports should be in ascii, not caseless, order. If we've done otherwise elsewhere I'll change it. The cast isn't the best but that's what we get for using a dict holding various objects -- the thing marked by `vhost` is going to be a virtualhost so 🤷 `re.findall` can [return](https://docs.python.org/3/library/re.html#re.findall) either a list of strings or a list of tuples of strings, depending what you specify in the regex. In our case it's going to be a list of strings. --- certbot-nginx/certbot_nginx/_internal/configurator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/certbot-nginx/certbot_nginx/_internal/configurator.py b/certbot-nginx/certbot_nginx/_internal/configurator.py index 50a7a9dc0..3ca133338 100644 --- a/certbot-nginx/certbot_nginx/_internal/configurator.py +++ b/certbot-nginx/certbot_nginx/_internal/configurator.py @@ -477,9 +477,9 @@ class NginxConfigurator(common.Configurator): # Wildcard match - need to find the longest one rank = matches[0]['rank'] wildcards = [x for x in matches if x['rank'] == rank] - return max(wildcards, key=lambda x: len(x['name']))['vhost'] + return cast(obj.VirtualHost, max(wildcards, key=lambda x: len(x['name']))['vhost']) # Exact or regex match - return matches[0]['vhost'] + return cast(obj.VirtualHost, matches[0]['vhost']) def _rank_matches_by_name(self, vhost_list: Iterable[obj.VirtualHost], target_name: str) -> List[Dict[str, Any]]: @@ -1118,7 +1118,7 @@ class NginxConfigurator(common.Configurator): """ text = self._nginx_version() - matches = re.findall(r"running with OpenSSL ([^ ]+) ", text) + matches: List[str] = re.findall(r"running with OpenSSL ([^ ]+) ", text) if not matches: matches = re.findall(r"built with OpenSSL ([^ ]+) ", text) if not matches: