Skip to content

Commit

Permalink
fix typing findings
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Aug 14, 2024
1 parent ba5034a commit 328b914
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ show_error_codes = true
pretty = true
strict = true

[[tool.mypy.overrides]]
module = [
"feedgen.*",
]
ignore_missing_imports = true

[tool.pyright]
include = ["src/pallets"]

Expand Down
16 changes: 16 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ sqlalchemy[asyncio]==2.0.32
# flask-sqlalchemy-lite
tox==4.18.0
# via -r dev.in
types-beautifulsoup4==4.12.0.20240511
# via -r typing.txt
types-docutils==0.21.0.20240724
# via
# -r typing.txt
# types-pygments
types-html5lib==1.1.11.20240806
# via
# -r typing.txt
# types-beautifulsoup4
types-pygments==2.18.0.20240506
# via -r typing.txt
types-setuptools==71.1.0.20240813
# via
# -r typing.txt
# types-pygments
typing-extensions==4.12.2
# via
# -r base.txt
Expand Down
2 changes: 2 additions & 0 deletions requirements/typing.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mypy
pyright
types-beautifulsoup4
types-pygments
10 changes: 10 additions & 0 deletions requirements/typing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ nodeenv==1.9.1
# via pyright
pyright==1.1.376
# via -r typing.in
types-beautifulsoup4==4.12.0.20240511
# via -r typing.in
types-docutils==0.21.0.20240724
# via types-pygments
types-html5lib==1.1.11.20240806
# via types-beautifulsoup4
types-pygments==2.18.0.20240506
# via -r typing.in
types-setuptools==71.1.0.20240813
# via types-pygments
typing-extensions==4.12.2
# via mypy
13 changes: 9 additions & 4 deletions src/pallets/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def render_link_open(
env: dict[str, t.Any],
) -> str:
token = tokens[index]
href = _rewrite_link(token.attrs.get("href"), env["content_dir"], True)
href = t.cast(str | None, token.attrs.get("href"))
href = _rewrite_link(href, env["content_dir"], True)

if href is not None:
token.attrs["href"] = href
Expand All @@ -67,7 +68,8 @@ def render_image(
env: dict[str, t.Any],
) -> str:
token = tokens[index]
src = _rewrite_link(token.attrs.get("src"), env["content_dir"])
src = t.cast(str | None, token.attrs.get("src"))
src = _rewrite_link(src, env["content_dir"])

if src is not None:
token.attrs["src"] = src
Expand All @@ -88,12 +90,15 @@ def render_html_block(

for attr, page in (("href", True), ("src", False)):
for tag in soup.find_all(**{attr: True}):
ref = _rewrite_link(tag[attr], env["content_dir"], page)
ref = t.cast(str | None, tag[attr])
ref = _rewrite_link(ref, env["content_dir"], page)

if ref is not None:
tag[attr] = ref

assert soup.html is not None
soup.html.unwrap()
assert soup.body is not None
soup.body.unwrap()
token.content = str(soup)
return renderer.html_block(tokens, index, options, env)
Expand Down Expand Up @@ -134,4 +139,4 @@ def _rewrite_link(


def render_content(src: str, path: str) -> str:
return md.render(src, {"content_dir": path})
return t.cast(str, md.render(src, {"content_dir": path}))
4 changes: 2 additions & 2 deletions src/pallets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BlogPost(BasePage):
content_prefix = "blog"
__tablename__ = "blog_post"
title: orm.Mapped[str]
content: orm.Mapped[str]
content: orm.Mapped[str] # pyright: ignore
author_name: orm.Mapped[str]
published: orm.Mapped[datetime]
updated: orm.Mapped[datetime]
Expand Down Expand Up @@ -115,7 +115,7 @@ def make_feed(cls) -> str:
rel="alternate",
)

_cached_feed = fg.atom_str(pretty=True).decode()
_cached_feed = t.cast(str, fg.atom_str(pretty=True).decode())
return _cached_feed


Expand Down
7 changes: 5 additions & 2 deletions src/pallets/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from __future__ import annotations

import typing as t
from pathlib import Path

import sqlalchemy as sa
from flask import Blueprint
from flask import current_app
from flask import redirect
from flask import render_template
from flask import Response
from flask import send_from_directory
from flask import url_for
from werkzeug import Response
from werkzeug.exceptions import NotFound

from . import db
Expand Down Expand Up @@ -93,5 +96,5 @@ def blog_post(path: str) -> str:


@bp.route("/blog/feed.xml")
def blog_feed():
def blog_feed() -> tuple[str, dict[str, t.Any]]:
return models.BlogPost.make_feed(), {"content-type": "application/atom+xml"}

0 comments on commit 328b914

Please sign in to comment.