Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
python: Fix lxml typing issues.
Anders Kaseorgb0afbe3e3141
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.pyindex 9533031b807..ec170658a60 100644--- a/zerver/lib/email_notifications.py+++ b/zerver/lib/email_notifications.py@@ -80,7 +80,7 @@ def relative_to_full_url(fragment: lxml.html.HtmlElement, base_url: str) -> Nonefor link_info in fragment.iterlinks():elem, attrib, link, _pos = link_infomatch = re.match(r"/?#narrow/", link)- if match is not None:+ if attrib is not None and match is not None:link = re.sub(r"^/?#narrow/", base_url + "/#narrow/", link)elem.set(attrib, link)# Only manually linked narrow URLs have title attribute set.@@ -97,8 +97,10 @@ def relative_to_full_url(fragment: lxml.html.HtmlElement, base_url: str) -> None# entire message body will be that image element; here, we need a# more drastic edit to the content.inner = fragment[0]- image_link = inner.find("a").get("href")- image_title = inner.find("a").get("title")+ a = inner.find("a")+ assert a is not None+ image_link = a.attrib["href"]+ image_title = a.get("title")title_attr = {} if image_title is None else {"title": image_title}inner.clear()inner.tag = "p"@@ -130,17 +132,18 @@ def relative_to_full_url(fragment: lxml.html.HtmlElement, base_url: str) -> Nonedef fix_emojis(fragment: lxml.html.HtmlElement, emojiset: str) -> None:- def make_emoji_img_elem(emoji_span_elem: lxml.html.HtmlElement) -> dict[str, Any]:+ def make_emoji_img_elem(emoji_span_elem: lxml.html.HtmlElement) -> lxml.html.HtmlElement:# Convert the emoji spans to img tags.- classes = emoji_span_elem.get("class")+ classes = emoji_span_elem.attrib["class"]match = re.search(r"emoji-(?P<emoji_code>\S+)", classes)# re.search is capable of returning None,# but since the parent function should only be called with a valid css element# we assert that it does not.assert match is not Noneemoji_code = match.group("emoji_code")- emoji_name = emoji_span_elem.get("title")+ emoji_name = emoji_span_elem.attrib["title"]alt_code = emoji_span_elem.text+ assert alt_code is not None# We intentionally do not use staticfiles_storage.url here, so# that we don't get any hashed version -- we want a path which# may give us content which changes over time, but one which@@ -163,6 +166,7 @@ def fix_emojis(fragment: lxml.html.HtmlElement, emojiset: str) -> None:for elem in fragment.cssselect("span.emoji"):parent = elem.getparent()+ assert parent is not Noneimg_elem = make_emoji_img_elem(elem)parent.replace(elem, img_elem)diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.pyindex 4bbc32d1f99..442cfcf2713 100644--- a/zerver/lib/push_notifications.py+++ b/zerver/lib/push_notifications.py@@ -5,7 +5,7 @@ import copyimport loggingimport reimport time-from collections.abc import Callable, Mapping, Sequence+from collections.abc import Callable, Iterable, Mapping, Sequencefrom dataclasses import asdict, dataclassfrom email.headerregistry import Addressfrom functools import cache@@ -871,7 +871,7 @@ def get_mobile_push_content(rendered_content: str) -> str:def render_olist(ol: lxml.html.HtmlElement) -> str:items = []- counter = int(ol.get("start")) if ol.get("start") else 1+ counter = int(ol.get("start", 1))nested_levels = sum(1 for ancestor in ol.iterancestors("ol"))indent = ("\n" + " " * nested_levels) if nested_levels else ""@@ -917,7 +917,7 @@ def get_mobile_push_content(rendered_content: str) -> str:message_link_elements = []anchor_elements = element.cssselect("a[href]")for elem in anchor_elements:- href = elem.get("href")+ href = elem.attrib["href"]if is_same_server_message_link(href):message_link_elements.append(elem)@@ -931,27 +931,28 @@ def get_mobile_push_content(rendered_content: str) -> str:)return remaining_text.strip() == ":"- def get_collapsible_status_array(elements: list[lxml.html.HtmlElement]) -> list[bool]:+ def get_collapsible_status_array(elements: Iterable[lxml.html.HtmlElement]) -> list[bool]:collapsible_status: list[bool] = [element.tag == "blockquote" or is_user_said_paragraph(element) for element in elements]return collapsible_statusdef potentially_collapse_quotes(element: lxml.html.HtmlElement) -> None:- children = element.getchildren()- collapsible_status = get_collapsible_status_array(children)+ collapsible_status = get_collapsible_status_array(element)if all(collapsible_status) or all(not x for x in collapsible_status):returncollapse_element = lxml.html.Element("p")collapse_element.text = "[…]"- for index, child in enumerate(children):+ for index, child in enumerate(element):if collapsible_status[index]:if index > 0 and collapsible_status[index - 1]:child.drop_tree()else:- child.getparent().replace(child, collapse_element)+ parent = child.getparent()+ assert parent is not None+ parent.replace(child, collapse_element)elem = lxml.html.fragment_fromstring(rendered_content, create_parent=True)change_katex_to_raw_latex(elem)diff --git a/zerver/views/documentation.py b/zerver/views/documentation.pyindex fe64df3046c..19c49eaa41a 100644--- a/zerver/views/documentation.py+++ b/zerver/views/documentation.py@@ -13,7 +13,8 @@ from django.template import loaderfrom django.template.response import TemplateResponsefrom django.views.generic import TemplateViewfrom lxml import html-from lxml.etree import Element, SubElement, XPath, _Element+from lxml.etree import XPath, _Element+from lxml.html import builder as efrom markupsafe import Markupfrom typing_extensions import override@@ -282,11 +283,11 @@ class MarkdownDirectoryView(ApiURLView):sidebar_html = ""tree = html.fragment_fromstring(sidebar_html, create_parent=True)if not context.get("page_is_policy_center", False):- home_h2 = Element("h2")- home_link = SubElement(home_h2, "a")- home_link.attrib["class"] = "no-underline"- home_link.attrib["href"] = context["doc_root"]- home_link.text = context["doc_root_title"] + " home"+ home_link = e.A(+ {"class": "no-underline", "href": context["doc_root"]},+ context["doc_root_title"] + " home",+ )+ home_h2 = e.H2(home_link)tree.insert(0, home_h2)url = context["doc_root"] + article# Remove ID attributes from sidebar headings so they don't conflict with index page headings@@ -301,7 +302,6 @@ class MarkdownDirectoryView(ApiURLView):for a in links:assert isinstance(a, _Element)old_class = a.attrib.get("class", "")- assert isinstance(old_class, str)a.attrib["class"] = old_class + " highlighted"context["sidebar_html"] = Markup().join(Markup(html.tostring(child, encoding="unicode")) for child in tree