66import json
77import typing
88import warnings
9- from collections .abc import Mapping
109from io import BytesIO
11- from re import Pattern
12- from typing import Any ,Literal ,SupportsIndex ,TypedDict ,TypeVar ,Union
10+ from typing import TYPE_CHECKING ,Any ,Literal ,SupportsIndex ,TypedDict ,TypeVar ,Union
1311
1412import jmespath
1513from lxml import etree ,html
1816from .csstranslator import GenericTranslator ,HTMLTranslator
1917from .utils import extract_regex ,flatten ,iflatten ,shorten
2018
19+ if TYPE_CHECKING :
20+ from collections .abc import Mapping
21+ from re import Pattern
22+
23+
2124_SelectorType = TypeVar ("_SelectorType" ,bound = "Selector" )
2225_ParserType = Union [etree .XMLParser ,etree .HTMLParser ]
2326# simplified _OutputMethodArg from types-lxml
@@ -50,7 +53,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
5053
5154
5255class CTGroupValue (TypedDict ):
53- _parser :type [etree .XMLParser ] | type [ html .HTMLParser ]
56+ _parser :type [etree .XMLParser | html .HTMLParser ]
5457_csstranslator :GenericTranslator | HTMLTranslator
5558_tostring_method :_TostringMethodType
5659
@@ -97,7 +100,8 @@ def create_root_node(
97100if "use XML_PARSE_HUGE option" in error .message :
98101warnings .warn (
99102f"Input data is too big. Upgrade to lxml "
100- f"{ lxml_huge_tree_version } or later for huge_tree support."
103+ f"{ lxml_huge_tree_version } or later for huge_tree support." ,
104+ stacklevel = 2 ,
101105 )
102106if root is None :
103107root = etree .fromstring (b"<html/>" ,parser = parser ,base_url = base_url )
@@ -124,8 +128,7 @@ def __getitem__(
124128o = super ().__getitem__ (pos )
125129if isinstance (pos ,slice ):
126130return self .__class__ (typing .cast ("SelectorList[_SelectorType]" ,o ))
127- else :
128- return typing .cast (_SelectorType ,o )
131+ return typing .cast (_SelectorType ,o )
129132
130133def __getstate__ (self )-> None :
131134raise TypeError ("can't pickle SelectorList objects" )
@@ -341,7 +344,7 @@ def _get_root_type(root: Any, *, input_type: str | None) -> str:
341344f"and{ input_type !r} as type."
342345 )
343346return _xml_or_html (input_type )
344- elif isinstance (root , (dict ,list ))or _is_valid_json (root ):
347+ if isinstance (root , (dict ,list ))or _is_valid_json (root ):
345348return "json"
346349return input_type or "json"
347350
@@ -392,14 +395,14 @@ class Selector:
392395 """
393396
394397__slots__ = [
395- "namespaces" ,
396- "type" ,
398+ "__weakref__" ,
397399"_expr" ,
398400"_huge_tree" ,
399- "root" ,
400401"_text" ,
401402"body" ,
402- "__weakref__" ,
403+ "namespaces" ,
404+ "root" ,
405+ "type" ,
403406 ]
404407
405408_default_namespaces = {
@@ -541,8 +544,7 @@ def jmespath(
541544def make_selector (x :Any )-> _SelectorType :# closure function
542545if isinstance (x ,str ):
543546return self .__class__ (text = x ,_expr = query ,type = "text" )
544- else :
545- return self .__class__ (root = x ,_expr = query )
547+ return self .__class__ (root = x ,_expr = query )
546548
547549result = [make_selector (x )for x in result ]
548550return typing .cast (SelectorList [_SelectorType ],self .selectorlist_cls (result ))
@@ -707,10 +709,9 @@ def get(self) -> Any:
707709except (AttributeError ,TypeError ):
708710if self .root is True :
709711return "1"
710- elif self .root is False :
712+ if self .root is False :
711713return "0"
712- else :
713- return str (self .root )
714+ return str (self .root )
714715
715716extract = get
716717