Skip to content

Commit

Permalink
Additional typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sciyoshi committed Jun 16, 2023
1 parent d43d56f commit 56e580d
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 33 deletions.
3 changes: 2 additions & 1 deletion prosemirror/model/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ def from_json(cls, schema: "Schema[str, str]", value: Any) -> "Fragment":
def from_array(cls, array: List["Node"]) -> "Fragment":
if not array:
return cls.empty
joined, size = None, 0
joined: Optional[List["Node"]] = None
size = 0
for i in range(len(array)):
node = array[i]
size += node.node_size
Expand Down
2 changes: 1 addition & 1 deletion prosemirror/model/from_dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def remove_pending_mark(self, mark: Mark, upto: NodeContext) -> None:

def normalize_list(dom_: DOMNode) -> None:
child = list(dom_)[0]
prev_item = None
prev_item: Optional[DOMNode] = None

while child is not None:
name = child.tag.lower() if get_node_type(child) == 1 else None
Expand Down
2 changes: 1 addition & 1 deletion prosemirror/model/mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, type: "MarkType", attrs: JSONDict) -> None:
self.attrs = attrs

def add_to_set(self, set: List["Mark"]) -> List["Mark"]:
copy = None
copy: Optional[List["Mark"]] = None
placed = False
for i in range(len(set)):
other = set[i]
Expand Down
2 changes: 1 addition & 1 deletion prosemirror/model/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def can_replace(
return True

def can_replace_with(
self, from_: int, to: int, type: "NodeType", marks: None = None
self, from_: int, to: int, type: "NodeType", marks: Optional[List[Mark]] = None
) -> bool:
if marks and not self.type.allows_marks(marks):
return False
Expand Down
2 changes: 1 addition & 1 deletion prosemirror/model/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def remove_range(content: Fragment, from_: int, to: int) -> Fragment:


def insert_into(
content: Fragment, dist: int, insert: Fragment, parent: None
content: Fragment, dist: int, insert: Fragment, parent: Optional["Node"]
) -> Optional[Fragment]:
a = content.find_index(dist)
index, offset = a["index"], a["offset"]
Expand Down
6 changes: 4 additions & 2 deletions prosemirror/model/resolvedpos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, List, Optional, Union, cast
from typing import TYPE_CHECKING, List, Optional, Union, cast, Callable

from .mark import Mark

Expand Down Expand Up @@ -144,7 +144,9 @@ def shared_depth(self, pos: int) -> int:
return 0

def block_range(
self, other: Optional["ResolvedPos"] = None, pred: None = None
self,
other: Optional["ResolvedPos"] = None,
pred: Optional[Callable[["Node"], bool]] = None,
) -> Optional["NodeRange"]:
if other is None:
other = self
Expand Down
5 changes: 1 addition & 4 deletions prosemirror/model/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

from .content import ContentMatch

if TYPE_CHECKING:
pass

Attrs: TypeAlias = JSONDict


Expand Down Expand Up @@ -197,7 +194,7 @@ def allows_marks(self, marks: List[Mark]) -> bool:
def allowed_marks(self, marks: List[Mark]) -> List[Mark]:
if self.mark_set is None:
return marks
copy = None
copy: Optional[List[Mark]] = None
for i, mark in enumerate(marks):
if not self.allows_mark_type(mark.type):
if not copy:
Expand Down
46 changes: 24 additions & 22 deletions prosemirror/model/to_dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Sequence,
Tuple,
Union,
Set,
cast,
)

Expand All @@ -27,27 +28,28 @@ def __str__(self) -> str:
return "".join([str(c) for c in self.children])


class Element(DocumentFragment):
self_closing_elements = frozenset(
[
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]
)
SELF_CLOSING_ELEMENTS = frozenset(
{
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
}
)


class Element(DocumentFragment):
def __init__(
self, name: str, attrs: Dict[str, str], children: List[HTMLNode]
) -> None:
Expand All @@ -58,7 +60,7 @@ def __init__(
def __str__(self) -> str:
attrs_str = " ".join([f'{k}="{html.escape(v)}"' for k, v in self.attrs.items()])
open_tag_str = " ".join([s for s in [self.name, attrs_str] if s])
if self.name in self.self_closing_elements:
if self.name in SELF_CLOSING_ELEMENTS:
assert not self.children, "self-closing elements should not have children"
return f"<{open_tag_str}>"
children_str = "".join([str(c) for c in self.children])
Expand Down Expand Up @@ -157,7 +159,7 @@ def render_spec(
tag_name = structure[0]
if " " in tag_name[1:]:
raise NotImplementedError("XML namespaces are not supported")
content_dom = None
content_dom: Optional[Element] = None
dom = Element(name=tag_name, attrs={}, children=[])
attrs = structure[1] if len(structure) > 1 else None
start = 1
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ select = [
"RUF",
]

[tool.mypy]
warn_return_any = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = "prosemirror.model.*"
disallow_untyped_defs = true
disallow_untyped_calls = true
disallow_incomplete_defs = true

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 56e580d

Please sign in to comment.