fix escape_all for Union types with strings

This commit is contained in:
ducklet 2020-11-10 21:35:06 +01:00
parent a7c5431ec1
commit 12c08f1026

View file

@ -314,7 +314,7 @@ class ElementParser(HTMLParser):
def escape_all(dc, escape: Callable[[str], str] = html_escape) -> None: def escape_all(dc, escape: Callable[[str], str] = html_escape) -> None:
"""Patch a dataclass to escape all strings.""" """Patch a dataclass to escape all strings."""
for f in fields(dc): for f in fields(dc):
if f.type is str: if type(getattr(dc, f.name)) is str:
setattr(dc, f.name, escape(getattr(dc, f.name))) setattr(dc, f.name, escape(getattr(dc, f.name)))
elif get_origin(f.type) is list and get_args(f.type)[0] is str: elif get_origin(f.type) is list and get_args(f.type)[0] is str:
setattr(dc, f.name, [escape(x) for x in getattr(dc, f.name)]) setattr(dc, f.name, [escape(x) for x in getattr(dc, f.name)])