Skip to content

Commit 3cea936

Browse files
authored
Print a different error if comments are found (#7)
1 parent bf512ab commit 3cea936

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ This makes sense in many cases, but possibly not every case.
213213

214214
## Changelog
215215

216+
### [0.0.7] - unreleased
217+
218+
* Warn if comments are found/don't treat comments as child elements in error messages
219+
216220
### [0.0.6] - 2020-03-25
217221

218222
* Allow ignored fields via `init=false` or the `ignored` function

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ good-names = "_,e,el,ex,f,tp,k,v,ns"
7777
indent-string = " "
7878

7979
[tool.taskipy.tasks]
80-
isort = "isort -v ."
80+
isort = "isort ."
8181
black = "black ."
8282
mypy = "mypy --strict src/xml_dataclasses/ functional/container_test.py"
8383
pylint = "pylint src/xml_dataclasses/"

src/xml_dataclasses/serde.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, Dict, List, Mapping, Optional, Type, TypeVar, Union
55

66
from lxml.builder import ElementMaker # type: ignore
7+
from lxml.etree import _Comment as Comment # type: ignore
78

89
from .lxml_utils import strip_ns
910
from .resolve_types import (
@@ -44,8 +45,10 @@ def _load_attributes(cls: Type[XmlDataclass], el: Any) -> Mapping[str, str]:
4445

4546

4647
def _load_text(info: TextInfo, el: Any) -> Mapping[str, str]:
47-
has_child = next(el.iterchildren(), None) is not None
48-
if has_child:
48+
child = next(el.iterchildren(), None)
49+
if child is not None:
50+
if isinstance(child, Comment):
51+
raise ValueError(f"Element '{el.tag}' contains comments")
4952
raise ValueError(f"Element '{el.tag}' has child elements (expected text only)")
5053

5154
text = el.text
@@ -64,6 +67,8 @@ def _load_children(cls: Type[XmlDataclass], el: Any) -> Mapping[str, XmlDataclas
6467
# child elements can be duplicated
6568
el_children: Dict[str, List[Any]] = defaultdict(list)
6669
for e in el.iterchildren():
70+
if isinstance(e, Comment):
71+
raise ValueError(f"Element '{el.tag}' contains comments")
6772
el_children[e.tag].append(e)
6873

6974
values = {}

tests/load_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,33 @@ def xml_validate(self) -> None:
549549
el = etree.fromstring('<foo bar="baz" />')
550550
with pytest.raises(MyError):
551551
load(Foo, el, "foo")
552+
553+
554+
def test_load_with_child_comment_not_stripped():
555+
@xml_dataclass
556+
class Foo:
557+
__ns__ = None
558+
bar: List[Union[Child1, Child2]]
559+
560+
el = etree.fromstring('<foo><!-- comment --><bar spam="eggs" /></foo>')
561+
562+
with pytest.raises(ValueError) as exc_info:
563+
load(Foo, el, "foo")
564+
565+
msg = str(exc_info.value)
566+
assert "Element 'foo' contains comments" in msg
567+
568+
569+
def test_load_with_text_comment_not_stripped():
570+
@xml_dataclass
571+
class Foo:
572+
__ns__ = None
573+
value: str = text()
574+
575+
el = etree.fromstring("<foo>spam<!-- comment -->eggs</foo>")
576+
577+
with pytest.raises(ValueError) as exc_info:
578+
load(Foo, el, "foo")
579+
580+
msg = str(exc_info.value)
581+
assert "Element 'foo' contains comments" in msg

0 commit comments

Comments
 (0)