Skip to content

Commit c39de35

Browse files
committed
restore python 3.9 compatibility
1 parent 2a9eac8 commit c39de35

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

htmlcompare/compare.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: MIT
22

33
from collections.abc import Sequence
4+
from typing import Optional
45

56
from htmlcompare.compare_css import compare_css
67
from htmlcompare.nodes import Comment, ConditionalComment, Document, Element, Node, TextNode
@@ -16,7 +17,7 @@
1617
def compare_html(
1718
expected_html: str,
1819
actual_html: str,
19-
options: CompareOptions | None = None,
20+
options: Optional[CompareOptions] = None,
2021
) -> ComparisonResult:
2122
"""
2223
Compare two HTML strings for equality.

htmlcompare/nodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from collections.abc import Sequence
44
from dataclasses import dataclass, field
5+
from typing import Union
56

67

78
__all__ = ['Node', 'Element', 'TextNode', 'Comment', 'ConditionalComment', 'Document']
@@ -74,4 +75,4 @@ def __eq__(self, other):
7475

7576

7677
# Type alias for any node type
77-
Node = Element | TextNode | Comment | ConditionalComment
78+
Node = Union[Element, TextNode, Comment, ConditionalComment]

htmlcompare/normalize.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: MIT
22

33
import re
4+
from typing import Optional
45

56
from htmlcompare.elements import is_block_element
67
from htmlcompare.nodes import Comment, ConditionalComment, Document, Element, Node, TextNode
@@ -13,7 +14,7 @@
1314
_DEFAULT_OPTIONS = CompareOptions()
1415

1516

16-
def normalize_tree(doc: Document, options: CompareOptions | None = None) -> Document:
17+
def normalize_tree(doc: Document, options: Optional[CompareOptions] = None) -> Document:
1718
"""
1819
Normalize a document tree for comparison.
1920
@@ -74,7 +75,7 @@ def _normalize_children(
7475
return result
7576

7677

77-
def _normalize_node(node: Node, in_block_context: bool, options: CompareOptions) -> Node | None:
78+
def _normalize_node(node: Node, in_block_context: bool, options: CompareOptions) -> Optional[Node]:
7879
"""
7980
Normalize a single node.
8081
@@ -92,7 +93,7 @@ def _normalize_node(node: Node, in_block_context: bool, options: CompareOptions)
9293
return node
9394

9495

95-
def _normalize_text_node(node: TextNode, in_block_context: bool) -> TextNode | None:
96+
def _normalize_text_node(node: TextNode, in_block_context: bool) -> Optional[TextNode]:
9697
"""
9798
Normalize a text node.
9899
@@ -152,7 +153,7 @@ def _normalize_element(element: Element, options: CompareOptions) -> Element:
152153
def _normalize_conditional_comment(
153154
node: ConditionalComment,
154155
options: CompareOptions,
155-
) -> ConditionalComment | None:
156+
) -> Optional[ConditionalComment]:
156157
"""
157158
Normalize a conditional comment.
158159

htmlcompare/parser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import xml.etree.ElementTree as ET
55
from collections.abc import Sequence
6+
from typing import Optional, Union
67

78
import html5lib
89

@@ -47,8 +48,8 @@ def _element_to_node(element) -> Element:
4748
return Element(tag=tag, attributes=attributes, children=children)
4849

4950

50-
def _convert_children(element) -> Sequence[Element | TextNode | Comment | ConditionalComment]:
51-
children: list[Element | TextNode | Comment | ConditionalComment] = []
51+
def _convert_children(element) -> Sequence[Union[Element, TextNode, Comment, ConditionalComment]]:
52+
children: list[Union[Element, TextNode, Comment, ConditionalComment]] = []
5253
if element.text:
5354
# leading text before any child elements
5455
children.append(TextNode(content=element.text))
@@ -75,7 +76,8 @@ def _is_comment(element) -> bool:
7576
return callable(element.tag) or element.tag == ET.Comment
7677

7778

78-
def _parse_conditional_comment(content: str) -> ConditionalComment | None:
79+
80+
def _parse_conditional_comment(content: str) -> Optional[ConditionalComment]:
7981
"""
8082
Parse an IE conditional comment if the content matches the pattern.
8183

htmlcompare/tests/parser_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SPDX-License-Identifier: MIT
22

3+
from typing import Optional
4+
35
from htmlcompare.nodes import Comment, ConditionalComment, Document, Element, TextNode
46
from htmlcompare.parser import parse_html as parse_html2
57

@@ -238,7 +240,7 @@ def test_regular_comment_not_parsed_as_conditional():
238240
assert comment.content == ' just a regular comment '
239241

240242

241-
def _find_first_child_with_tag(element: Element, tag: str) -> Element | None:
243+
def _find_first_child_with_tag(element: Element, tag: str) -> Optional[Element]:
242244
for child in element.children:
243245
if isinstance(child, Element) and child.tag == tag:
244246
return child

htmlcompare/testutils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SPDX-License-Identifier: MIT
22

3+
from typing import Optional
4+
35
from htmlcompare.compare import compare_html
46
from htmlcompare.options import CompareOptions
57
from htmlcompare.result import ComparisonResult
@@ -12,8 +14,8 @@ def assert_same_html(
1214
expected_html: str,
1315
actual_html: str,
1416
verbose: bool = True,
15-
message: str | None = None,
16-
options: CompareOptions | None = None,
17+
message: Optional[str] = None,
18+
options: Optional[CompareOptions] = None,
1719
) -> None:
1820
"""Assert that two HTML strings are semantically equal."""
1921
result = compare_html(expected_html, actual_html, options)
@@ -34,7 +36,7 @@ def assert_same_html(
3436
def assert_different_html(
3537
expected_html: str,
3638
actual_html: str,
37-
options: CompareOptions | None = None,
39+
options: Optional[CompareOptions] = None,
3840
) -> ComparisonResult:
3941
"""Assert that two HTML strings are semantically different."""
4042
result = compare_html(expected_html, actual_html, options)

0 commit comments

Comments
 (0)