From 9aabcf7fdafab6ad171d24c2c1c4c41612457786 Mon Sep 17 00:00:00 2001 From: Trevor Bergeron Date: Wed, 25 May 2022 19:19:38 -0400 Subject: [PATCH] squashme: conditional typing imports, suggestions --- sopel/modules/url.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/sopel/modules/url.py b/sopel/modules/url.py index 92126cfe13..c34c4dbf2a 100644 --- a/sopel/modules/url.py +++ b/sopel/modules/url.py @@ -13,7 +13,7 @@ from ipaddress import ip_address import logging import re -from typing import Generator, List, Optional, Tuple +from typing import TYPE_CHECKING from urllib.parse import urlparse import dns.resolver @@ -21,11 +21,15 @@ from urllib3.exceptions import LocationValueError # type: ignore[import] from sopel import plugin, tools -from sopel.bot import Sopel, SopelWrapper -from sopel.config import Config, types +from sopel.config import types from sopel.tools import web -from sopel.trigger import Trigger +if TYPE_CHECKING: + from typing import Generator, List, Optional, Tuple + + from sopel.bot import Sopel, SopelWrapper + from sopel.config import Config + from sopel.trigger import Trigger LOGGER = logging.getLogger(__name__) USER_AGENT = ( @@ -320,6 +324,11 @@ def process_urls( """ For each URL in the list, ensure it should be titled, and do so. + :param bot: Sopel instance + :param trigger: The trigger object for this event + :param urls: The URLs detected in the triggering message + :param requested: Whether the title was explicitly requested (vs automatic) + See if it's handled by another plugin. If not, find where it redirects to, if anywhere. If that redirected URL should be handled by another plugin, dispatch the callback for it. Return a list of @@ -329,11 +338,6 @@ def process_urls( For titles explicitly requested by the user, exclusion_char and excludes are skipped. - - :param bot: Sopel instance - :param trigger: The trigger object for this event - :param urls: The URLs detected in the triggering message - :param requested: Whether the title was explicitly requested (vs automatic) """ shorten_url_length = bot.config.url.shorten_url_length for url in urls: @@ -406,9 +410,9 @@ def check_callbacks(bot: SopelWrapper, url: str, use_excludes: bool = True) -> b """ # Check if it matches the exclusion list first - excluded = False - if use_excludes: - excluded = any(regex.search(url) for regex in bot.memory["url_exclude"]) + excluded = use_excludes and any( + regex.search(url) for regex in bot.memory["url_exclude"] + ) return ( excluded or any(bot.search_url_callbacks(url)) or