1+ import sys
12import xml .etree .ElementTree as ET
23from collections .abc import Mapping
34from copy import deepcopy
4- from typing import Any , Optional , Self
55from xml .dom .minidom import parseString
66
77from .utils import safe_eval , sort_dict
88
9+ if sys .version_info < (3 , 11 ):
10+ from typing import Any , Dict , Optional , TypeVar , Union
11+ Self = TypeVar ('Self' , bound = 'XmlElement' )
12+ else :
13+ from typing import Any , Dict , Optional , Self , Union
14+
915
1016class _list (list ):
1117 pass
@@ -29,7 +35,7 @@ def makeelement(cls, tag, attrib, text=None, tail=None) -> Self:
2935 return cls (tag , attrib , text , tail )
3036
3137 @classmethod
32- def clone (cls , src : Self | ET .Element , dst : Optional [Self ] = None ) -> Self :
38+ def clone (cls , src : Union [ Self , ET .Element ] , dst : Optional [Self ] = None ) -> Self :
3339 if dst is None :
3440 dst = cls (src .tag , src .attrib , src .text , src .tail )
3541
@@ -46,7 +52,7 @@ def from_xml(cls, xml: str) -> Self:
4652
4753 def to_xml (self ,
4854 minimal : Optional [bool ] = None ,
49- indent : Optional [int | str ] = 2
55+ indent : Optional [Union [ int , str ] ] = 2
5056 ) -> str :
5157 ele = deepcopy (self )
5258 for e in ele .iter ():
@@ -73,7 +79,7 @@ def spawn(self, tag: str, attrib={}, text=None, tail=None, **extra) -> Self:
7379 self .append (child )
7480 return child
7581
76- def _get_attrs (self ) -> Optional [dict [str , Any ]]:
82+ def _get_attrs (self ) -> Optional [Dict [str , Any ]]:
7783 attrs = {f'@{ k } ' : safe_eval (v ) for k , v in self .attrib .items ()}
7884
7985 if len (self ) == 0 :
0 commit comments