Skip to content

Commit c4dbf6e

Browse files
committed
Trial ty for type checking, with Python 3.10 TypedDict and TypeAlias and other syntax
1 parent 0ce4962 commit c4dbf6e

23 files changed

+361
-603
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ jobs:
2020
python-version-file: "pyproject.toml"
2121
- uses: astral-sh/setup-uv@v7
2222
- run: uv sync --dev
23-
- run: uv run make lint
23+
- run: uv run ruff check --output-format=github .
24+
- run: uv run ruff format --check
25+
- run: uv run mypy draftjs_exporter tests
26+
- run: uv run ty check --output-format=github .
2427
- run: uv run make benchmark
2528
- run: make build
2629
- uses: actions/setup-node@v6

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ init: clean-pyc ## Install dependencies and initialise for development.
1313
lint: ## Lint the project.
1414
ruff check
1515
ruff format --check
16-
mypy **/*.py
16+
mypy draftjs_exporter tests
17+
ty check
1718

1819
format: ## Format project files.
1920
ruff format

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ from draftjs_exporter.dom import DOM
8282
config = {
8383
# `block_map` is a mapping from Draft.js block types to a definition of their HTML representation.
8484
# Extend BLOCK_MAP to start with sane defaults, or make your own from scratch.
85-
'block_map': dict(BLOCK_MAP, **{
85+
'block_map': {
86+
**BLOCK_MAP,
8687
# The most basic mapping format, block type to tag name.
8788
BLOCK_TYPES.HEADER_TWO: 'h2',
8889
# Use a dict to define props on the block.
@@ -101,17 +102,18 @@ config = {
101102
},
102103
# Provide a fallback component (advanced).
103104
BLOCK_TYPES.FALLBACK: block_fallback
104-
}),
105+
},
105106
# `style_map` defines the HTML representation of inline elements.
106107
# Extend STYLE_MAP to start with sane defaults, or make your own from scratch.
107-
'style_map': dict(STYLE_MAP, **{
108+
'style_map': {
109+
**STYLE_MAP,
108110
# Use the same mapping format as in the `block_map`.
109111
'KBD': 'kbd',
110112
# The `style` prop can be defined as a dict, that will automatically be converted to a string.
111113
'HIGHLIGHT': {'element': 'strong', 'props': {'style': {'textDecoration': 'underline'}}},
112114
# Provide a fallback component (advanced).
113115
INLINE_STYLES.FALLBACK: style_fallback,
114-
}),
116+
},
115117
'entity_decorators': {
116118
# Map entities to components so they can be rendered with their data.
117119
ENTITY_TYPES.IMAGE: image,
@@ -206,10 +208,11 @@ Add the following to the exporter config,
206208

207209
```python
208210
config = {
209-
'block_map': dict(BLOCK_MAP, **{
211+
'block_map': {
212+
**BLOCK_MAP,
210213
# Provide a fallback for block types.
211214
BLOCK_TYPES.FALLBACK: block_fallback
212-
}),
215+
},
213216
}
214217
```
215218

benchmark.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import os
44
import re
55
from pstats import Stats
6+
from typing import cast
67

78
import memray
89
from markov_draftjs import get_content_sample
910

1011
from draftjs_exporter.constants import BLOCK_TYPES, ENTITY_TYPES
1112
from draftjs_exporter.defaults import BLOCK_MAP, STYLE_MAP
1213
from draftjs_exporter.dom import DOM
13-
from draftjs_exporter.html import HTML
14-
from draftjs_exporter.types import Element, Props
14+
from draftjs_exporter.html import HTML, ExporterConfig
15+
from draftjs_exporter.types import ContentState, Element, Props
1516
from example import br, entity_fallback, image, list_item, ordered_list
1617

1718

@@ -34,27 +35,25 @@ def block_fallback(props: Props) -> Element:
3435
return DOM.create_element("div", {}, props["children"])
3536

3637

37-
config = {
38-
"block_map": dict(
39-
BLOCK_MAP,
40-
**{
41-
BLOCK_TYPES.HEADER_TWO: "h2",
42-
BLOCK_TYPES.HEADER_THREE: {
43-
"element": "h3",
44-
"props": {"class": "u-text-center"},
45-
},
46-
BLOCK_TYPES.UNORDERED_LIST_ITEM: {
47-
"element": "li",
48-
"wrapper": "ul",
49-
"wrapper_props": {"class": "bullet-list"},
50-
},
51-
BLOCK_TYPES.ORDERED_LIST_ITEM: {
52-
"element": list_item,
53-
"wrapper": ordered_list,
54-
},
55-
BLOCK_TYPES.FALLBACK: block_fallback,
38+
config: ExporterConfig = {
39+
"block_map": {
40+
**BLOCK_MAP,
41+
BLOCK_TYPES.HEADER_TWO: "h2",
42+
BLOCK_TYPES.HEADER_THREE: {
43+
"element": "h3",
44+
"props": {"class": "u-text-center"},
5645
},
57-
),
46+
BLOCK_TYPES.UNORDERED_LIST_ITEM: {
47+
"element": "li",
48+
"wrapper": "ul",
49+
"wrapper_props": {"class": "bullet-list"},
50+
},
51+
BLOCK_TYPES.ORDERED_LIST_ITEM: {
52+
"element": list_item,
53+
"wrapper": ordered_list,
54+
},
55+
BLOCK_TYPES.FALLBACK: block_fallback,
56+
},
5857
"style_map": STYLE_MAP,
5958
"entity_decorators": {
6059
ENTITY_TYPES.IMAGE: image,
@@ -70,7 +69,8 @@ def block_fallback(props: Props) -> Element:
7069

7170
exporter = HTML(config)
7271

73-
content_states = get_content_sample()
72+
# markov_draftjs has slightly different type declarations.
73+
content_states = cast(list[ContentState], get_content_sample())
7474

7575
BENCHMARK_RUNS = int(os.environ.get("BENCHMARK_RUNS", 1))
7676

draftjs_exporter/defaults.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from draftjs_exporter.constants import BLOCK_TYPES, INLINE_STYLES
22
from draftjs_exporter.dom import DOM
3-
from draftjs_exporter.types import Element, Props
3+
from draftjs_exporter.types import ConfigMap, Element, Props
44

55

66
def render_children(props: Props) -> Element:
@@ -18,7 +18,7 @@ def code_block(props: Props) -> Element:
1818

1919

2020
# Default block map to extend.
21-
BLOCK_MAP = {
21+
BLOCK_MAP: ConfigMap = {
2222
BLOCK_TYPES.UNSTYLED: "p",
2323
BLOCK_TYPES.HEADER_ONE: "h1",
2424
BLOCK_TYPES.HEADER_TWO: "h2",
@@ -38,7 +38,7 @@ def code_block(props: Props) -> Element:
3838
# Tags come from https://developer.mozilla.org/en-US/docs/Web/HTML/Element.
3939
# and are loosely aligned with https://github.com/jpuri/draftjs-to-html.
4040
# Only styles that map to HTML elements are allowed as defaults.
41-
STYLE_MAP = {
41+
STYLE_MAP: ConfigMap = {
4242
INLINE_STYLES.BOLD: "strong",
4343
INLINE_STYLES.CODE: "code",
4444
INLINE_STYLES.ITALIC: "em",

draftjs_exporter/engines/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Any
1+
from typing import Any, TypeAlias
22

33
from draftjs_exporter.types import HTML, Element, Tag
44

5-
Attr = dict[str, str]
5+
Attr: TypeAlias = dict[str, str]
66

77

88
class DOMEngine:

draftjs_exporter/engines/html5lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_tag(type_: Tag, attr: Attr | None = None) -> Element:
2525
if not attr:
2626
attr = {}
2727

28-
return soup.new_tag(type_, **attr)
28+
return soup.new_tag(type_, attrs=attr)
2929

3030
@staticmethod
3131
def parse_html(markup: HTML) -> Element:

draftjs_exporter/engines/lxml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
from lxml import etree, html
3+
from lxml import etree, html # type: ignore
44

55
from draftjs_exporter.engines.base import Attr, DOMEngine
66
from draftjs_exporter.types import HTML, Tag

draftjs_exporter/engines/string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Elt:
3535
def __init__(self, type_: Tag, attr: Attr | None, markup: HTML = ""):
3636
self.type = type_
3737
self.attr = attr
38-
self.children: list["Elt"] = []
38+
self.children: list[str | "Elt"] = []
3939
self.markup = markup
4040

4141
@staticmethod

draftjs_exporter/entity_state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from draftjs_exporter.dom import DOM
44
from draftjs_exporter.error import ExporterException
55
from draftjs_exporter.options import Options, OptionsMap
6-
from draftjs_exporter.types import Block, Element, EntityDetails, EntityKey, EntityMap
6+
from draftjs_exporter.types import Block, Element, Entity, EntityKey, EntityMap
77

88

99
class EntityException(ExporterException):
@@ -44,7 +44,7 @@ def has_entity(self) -> list[EntityKey]:
4444
def has_no_entity(self) -> bool:
4545
return not self.entity_stack
4646

47-
def get_entity_details(self, entity_key: EntityKey) -> EntityDetails:
47+
def get_entity_details(self, entity_key: EntityKey) -> Entity:
4848
details = self.entity_map.get(entity_key)
4949

5050
if details is None:

0 commit comments

Comments
 (0)