Skip to content

Commit 361f7fc

Browse files
bhrutledgedi
andauthored
Fix renderer ignore (#230)
* 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 <[email protected]>
1 parent d30282f commit 361f7fc

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ build-backend = "setuptools.build_meta:__legacy__"
55
[tool.mypy]
66
strict = true
77
warn_unused_configs = true
8+
show_error_codes = true
9+
enable_error_code = [
10+
"ignore-without-code"
11+
]
12+
813
[[tool.mypy.overrides]]
914
# These modules do not yet have types available.
1015
module = [

readme_renderer/markdown.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import re
1717
import warnings
18-
from typing import Any, Match, Optional
18+
from typing import cast, Any, Dict, Callable, Match, Optional
1919

2020
from html import unescape
2121

@@ -33,13 +33,13 @@
3333
try:
3434
import cmarkgfm
3535
from cmarkgfm.cmark import Options as cmarkgfmOptions
36-
variants = {
37-
"GFM": lambda raw: cmarkgfm.github_flavored_markdown_to_html(
36+
variants: Dict[str, Callable[[str], str]] = {
37+
"GFM": lambda raw: cast(str, cmarkgfm.github_flavored_markdown_to_html(
3838
raw, options=cmarkgfmOptions.CMARK_OPT_UNSAFE
39-
),
40-
"CommonMark": lambda raw: cmarkgfm.markdown_to_html(
39+
)),
40+
"CommonMark": lambda raw: cast(str, cmarkgfm.markdown_to_html(
4141
raw, options=cmarkgfmOptions.CMARK_OPT_UNSAFE
42-
),
42+
)),
4343
}
4444
except ImportError:
4545
warnings.warn(_EXTRA_WARNING)
@@ -66,8 +66,7 @@ def render(
6666
if not renderer:
6767
return None
6868

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

7271
if not rendered:
7372
return None

0 commit comments

Comments
 (0)