Skip to content

Commit 8f55285

Browse files
committed
Clarify typing of _tostring_method.
1 parent c8aab66 commit 8f55285

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

parsel/selector.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Any,
99
Dict,
1010
List,
11+
Literal,
1112
Mapping,
1213
Optional,
1314
Pattern,
@@ -32,6 +33,10 @@
3233

3334
_SelectorType = TypeVar("_SelectorType", bound="Selector")
3435
_ParserType = Union[etree.XMLParser, etree.HTMLParser]
36+
_TostringMethodType = Literal[ # simplified _OutputMethodArg from types-lxml
37+
"html",
38+
"xml",
39+
]
3540

3641
lxml_version = parse_version(etree.__version__)
3742
lxml_huge_tree_version = parse_version("4.2")
@@ -341,8 +346,8 @@ def __init__(
341346
self._csstranslator: OriginalGenericTranslator = typing.cast(
342347
OriginalGenericTranslator, _ctgroup[st]["_csstranslator"]
343348
)
344-
self._tostring_method: str = typing.cast(
345-
str, _ctgroup[st]["_tostring_method"]
349+
self._tostring_method: _TostringMethodType = typing.cast(
350+
_TostringMethodType, _ctgroup[st]["_tostring_method"]
346351
)
347352

348353
if text is not None:
@@ -508,14 +513,11 @@ def get(self) -> str:
508513
Percent encoded content is unquoted.
509514
"""
510515
try:
511-
return typing.cast(
512-
str,
513-
etree.tostring(
514-
self.root,
515-
method=self._tostring_method,
516-
encoding="unicode",
517-
with_tail=False,
518-
),
516+
return etree.tostring(
517+
self.root,
518+
method=self._tostring_method,
519+
encoding="unicode",
520+
with_tail=False,
519521
)
520522
except (AttributeError, TypeError):
521523
if self.root is True:

0 commit comments

Comments
 (0)