Skip to content

Commit

Permalink
Fix renderer ignore (#230)
Browse files Browse the repository at this point in the history
* Require error codes for `type: ignore`

* Fix `type: ignore` of renderer variants

Based on:
https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas

mypy can infer lambda types; it seems we just need to annotate the
lookup dict.

* Use `cast` instead of `Any`

This isn't necessary to pass mypy, but offers a little more clarity.

Co-authored-by: Dustin Ingram <di@users.noreply.github.com>
  • Loading branch information
bhrutledge and di authored Mar 14, 2022
1 parent d30282f commit 361f7fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ build-backend = "setuptools.build_meta:__legacy__"
[tool.mypy]
strict = true
warn_unused_configs = true
show_error_codes = true
enable_error_code = [
"ignore-without-code"
]

[[tool.mypy.overrides]]
# These modules do not yet have types available.
module = [
Expand Down
15 changes: 7 additions & 8 deletions readme_renderer/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import re
import warnings
from typing import Any, Match, Optional
from typing import cast, Any, Dict, Callable, Match, Optional

from html import unescape

Expand All @@ -33,13 +33,13 @@
try:
import cmarkgfm
from cmarkgfm.cmark import Options as cmarkgfmOptions
variants = {
"GFM": lambda raw: cmarkgfm.github_flavored_markdown_to_html(
variants: Dict[str, Callable[[str], str]] = {
"GFM": lambda raw: cast(str, cmarkgfm.github_flavored_markdown_to_html(
raw, options=cmarkgfmOptions.CMARK_OPT_UNSAFE
),
"CommonMark": lambda raw: cmarkgfm.markdown_to_html(
)),
"CommonMark": lambda raw: cast(str, cmarkgfm.markdown_to_html(
raw, options=cmarkgfmOptions.CMARK_OPT_UNSAFE
),
)),
}
except ImportError:
warnings.warn(_EXTRA_WARNING)
Expand All @@ -66,8 +66,7 @@ def render(
if not renderer:
return None

# The renderer is a lambda function, and mypy fails lambdas right now.
rendered = renderer(raw) # type: ignore
rendered = renderer(raw)

if not rendered:
return None
Expand Down

0 comments on commit 361f7fc

Please sign in to comment.