From f267db9148e3a390a1a2e50fe71ac4675dceaea1 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Wed, 18 Nov 2026 21:28:28 +0100 Subject: refactor(core): tighten static typing Add explicit runtime narrowing in tests and simplify Huey decorator annotations so static analysis reflects the dynamic task wrapper behavior. Clean up URL argument construction and form error updates while preserving existing application behavior. --- tests/views/test_sources.py | 46 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'tests/views/test_sources.py') diff --git a/tests/views/test_sources.py b/tests/views/test_sources.py index c851833..a41c18c 100644 --- a/tests/views/test_sources.py +++ b/tests/views/test_sources.py @@ -12,7 +12,7 @@ from unittest.mock import Mock from urllib.parse import parse_qs, urlsplit import pytest -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup, Tag from flask import Flask from flask.testing import FlaskClient @@ -33,6 +33,13 @@ from .helpers import ( ) +def _href(tag: Tag) -> str: + """Return an HTML link target, checking BeautifulSoup's attribute type.""" + href = tag.get("href") + assert isinstance(href, str) + return unescape(href) + + @pytest.fixture def sources_module(app: Flask) -> ModuleType: _ = app @@ -81,10 +88,12 @@ def test_sent_sources_table_hides_secondary_columns_on_mobile( assert len(headers) == len(cells) == 7 for index in (2, 4, 5, 6): # URL, revision, created, last seen for cell in (headers[index], cells[index]): - assert {"d-none", "d-md-table-cell"} <= set(cell.get("class", [])) + assert {"d-none", "d-md-table-cell"} <= set( + cell.get_attribute_list("class") + ) for index in (0, 1, 3): # Path, count, status stay visible - assert "d-none" not in cells[index].get("class", []) - assert "text-break" in cells[1].get("class", []) + assert "d-none" not in cells[index].get_attribute_list("class") + assert "text-break" in cells[1].get_attribute_list("class") def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None: @@ -128,9 +137,11 @@ def test_sent_webmentions_table_keeps_status_and_actions_on_mobile( assert len(headers) == len(cells) == 7 for index in (2, 3, 4, 5): # Active, revisions, delivery, activity for cell in (headers[index], cells[index]): - assert {"d-none", "d-md-table-cell"} <= set(cell.get("class", [])) + assert {"d-none", "d-md-table-cell"} <= set( + cell.get_attribute_list("class") + ) for index in (0, 1, 6): # Target, status, resend action stay visible - assert "d-none" not in cells[index].get("class", []) + assert "d-none" not in cells[index].get_attribute_list("class") assert cells[6].select_one("button[data-action]") is not None @@ -380,9 +391,11 @@ def test_sources_filter_by_delivery_state_without_duplicate_rows( response = client.get("/sources", query_string={"status": status}) assert response.status_code == HTTPStatus.OK soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") - paths = [ - row.select_one("td a").get_text(strip=True) for row in soup.select("tbody tr") - ] + paths = [] + for row in soup.select("tbody tr"): + link = row.select_one("td a") + assert link is not None + paths.append(link.get_text(strip=True)) assert set(paths) == {f"blog/{name}/index.html" for name in expected} assert len(paths) == len(expected) assert ( @@ -458,9 +471,9 @@ def test_sources_sort_links_preserve_filters( soup = BeautifulSoup(html, "html.parser") assert soup.select_one('th[aria-sort="ascending"]') is not None links = [ - parse_qs(urlsplit(unescape(a["href"])).query) + parse_qs(urlsplit(_href(a)).query) for a in soup.select("th a[href]") - if urlsplit(unescape(a["href"])).path == "/sources" + if urlsplit(_href(a)).path == "/sources" ] assert len(links) == 7 for link in links: @@ -495,10 +508,9 @@ def test_sources_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) response = client.get("/sources", query_string={"sort": sort, "order": order}) assert response.status_code == HTTPStatus.OK soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") - assert ( - soup.select_one("tbody tr td a").get_text(strip=True) - == f"blog/{expected_first}/index.html" - ) + link = soup.select_one("tbody tr td a") + assert link is not None + assert link.get_text(strip=True) == f"blog/{expected_first}/index.html" def test_sources_filter_pagination_and_navigation_preserve_parameters( @@ -520,7 +532,7 @@ def test_sources_filter_pagination_and_navigation_preserve_parameters( assert "28 sources" in html soup = BeautifulSoup(html, "html.parser") assert len(soup.select("tbody tr")) == 10 - urls = [unescape(a["href"]) for a in soup.select("a[href]")] + urls = [_href(a) for a in soup.select("a[href]")] assert any( urlsplit(url).path == "/sources" and parse_qs(urlsplit(url).query) @@ -541,7 +553,7 @@ def test_sources_filter_pagination_and_navigation_preserve_parameters( 'a[href*="/sources?"]' ) assert back is not None - assert parse_qs(urlsplit(unescape(back["href"])).query) == { + assert parse_qs(urlsplit(_href(back)).query) == { **{k: [v] for k, v in filters.items()}, "page": ["2"], } -- cgit v1.3.1