1
- from _typeshed import Incomplete , ReadableBuffer , SupportsRead , SupportsWrite
2
- from collections import OrderedDict
3
- from collections .abc import Mapping
4
- from types import GeneratorType
5
- from typing import Any , Final , overload
6
-
7
- __author__ : Final [str ]
8
- __version__ : Final [str ]
9
- __license__ : Final [str ]
1
+ from _typeshed import ReadableBuffer , SupportsRead , SupportsWrite
2
+ from collections .abc import Callable , Container , Generator , Mapping
3
+ from typing import Any , overload
4
+ from typing_extensions import TypeAlias
10
5
11
6
class ParsingInterrupted (Exception ): ...
12
7
8
+ # dict as attribute value is exclusive to xmlns: https://github.com/bigpick/xmltodict/commit/22541b4874365cb8d2397f23087a866b3081fd9c
9
+ _AttrValue : TypeAlias = str | dict [str , str ]
10
+ _AttrDict : TypeAlias = dict [str , _AttrValue ]
11
+
12
+ class _DictSAXHandler :
13
+ path : list [tuple [str , _AttrDict | None ]]
14
+ stack : list [tuple [_AttrDict | None , list [str ]]]
15
+ data : list [str ]
16
+ item : _AttrDict | None
17
+ item_depth : int
18
+ xml_attribs : bool
19
+ item_callback : Callable [[list [tuple [str , _AttrDict | None ]], str | _AttrDict | None ], bool ]
20
+ attr_prefix : str
21
+ cdata_key : str
22
+ force_cdata : bool | Container [str ] | Callable [[tuple [str , _AttrDict | None ], str , str ], bool ]
23
+ cdata_separator : str
24
+ postprocessor : Callable [[list [tuple [str , _AttrDict | None ]], str , _AttrValue ], tuple [str , _AttrValue ]] | None
25
+ dict_constructor : type
26
+ strip_whitespace : bool
27
+ namespace_separator : str
28
+ namespaces : dict [str , str ] | None
29
+ namespace_declarations : dict [str , str ]
30
+ force_list : bool | Container [str ] | Callable [[tuple [str , _AttrDict | None ], str , str ], bool ] | None
31
+ comment_key : str
32
+ def __init__ (
33
+ self ,
34
+ item_depth : int = 0 ,
35
+ item_callback : Callable [[list [tuple [str , _AttrDict | None ]], str | _AttrDict | None ], bool ] = ...,
36
+ xml_attribs : bool = True ,
37
+ attr_prefix : str = "@" ,
38
+ cdata_key : str = "#text" ,
39
+ force_cdata : bool | Container [str ] | Callable [[tuple [str , _AttrDict | None ], str , str ], bool ] = False ,
40
+ cdata_separator : str = "" ,
41
+ postprocessor : Callable [[list [tuple [str , _AttrDict | None ]], str , _AttrValue ], tuple [str , _AttrValue ]] | None = None ,
42
+ dict_constructor : type = ...,
43
+ strip_whitespace : bool = True ,
44
+ namespace_separator : str = ":" ,
45
+ namespaces : dict [str , str ] | None = None ,
46
+ force_list : bool | Container [str ] | Callable [[tuple [str , _AttrDict | None ], str , str ], bool ] | None = None ,
47
+ comment_key : str = "#comment" ,
48
+ ) -> None : ...
49
+ def startNamespaceDecl (self , prefix : str , uri : str ) -> None : ...
50
+ def startElement (self , full_name : str , attrs : dict [str , str ] | list [str ]) -> None : ...
51
+ def endElement (self , full_name : str ) -> None : ...
52
+ def characters (self , data : str ) -> None : ...
53
+ def comments (self , data : str ) -> None : ...
54
+ def push_data (self , item : _AttrDict | None , key : str , data : str ) -> _AttrDict : ...
55
+
13
56
def parse (
14
- xml_input : str | ReadableBuffer | SupportsRead [bytes ] | GeneratorType [ReadableBuffer , Any , Any ],
57
+ xml_input : str | ReadableBuffer | SupportsRead [bytes ] | Generator [ReadableBuffer ],
15
58
encoding : str | None = None ,
16
59
expat : Any = ...,
17
60
process_namespaces : bool = False ,
@@ -20,19 +63,19 @@ def parse(
20
63
process_comments : bool = False ,
21
64
* ,
22
65
item_depth : int = 0 ,
23
- item_callback = ...,
66
+ item_callback : Callable [[ list [ tuple [ str , _AttrDict | None ]], str | _AttrDict | None ], bool ] = ...,
24
67
xml_attribs : bool = True ,
25
- attr_prefix = "@" ,
26
- cdata_key = "#text" ,
27
- force_cdata : bool | Incomplete = False ,
28
- cdata_separator = "" ,
29
- postprocessor = None ,
68
+ attr_prefix : str = "@" ,
69
+ cdata_key : str = "#text" ,
70
+ force_cdata : bool | Container [ str ] | Callable [[ tuple [ str , _AttrDict | None ], str , str ], bool ] = False ,
71
+ cdata_separator : str = "" ,
72
+ postprocessor : Callable [[ list [ tuple [ str , _AttrDict | None ]], str , _AttrValue ], tuple [ str , _AttrValue ]] | None = None ,
30
73
dict_constructor : type = ...,
31
74
strip_whitespace : bool = True ,
32
- namespaces = None ,
33
- force_list : bool | Incomplete = None ,
75
+ namespaces : dict [ str , str ] | None = None ,
76
+ force_list : bool | Container [ str ] | Callable [[ tuple [ str , _AttrDict | None ], str , str ], bool ] | None = None ,
34
77
comment_key : str = "#comment" ,
35
- ) -> OrderedDict [str , Any ]: ...
78
+ ) -> dict [str , Any ]: ...
36
79
@overload
37
80
def unparse (
38
81
input_dict : Mapping [str , Any ],
@@ -43,15 +86,17 @@ def unparse(
43
86
comment_key : str = "#comment" ,
44
87
* ,
45
88
attr_prefix : str = "@" ,
46
- cdata_key = "#text" ,
89
+ cdata_key : str = "#text" ,
47
90
depth : int = 0 ,
48
- preprocessor = None ,
91
+ # preprocessor is called like (preprocessor(key, value) for key, value in input_dict.items()).
92
+ # It is expected to return its input, or a modification thereof
93
+ preprocessor : Callable [[str , Any ], tuple [str , Any ]] | None = None ,
49
94
pretty : bool = False ,
50
95
newl : str = "\n " ,
51
96
indent : str | int = "\t " ,
52
97
namespace_separator : str = ":" ,
53
- namespaces = None ,
54
- expand_iter = None ,
98
+ namespaces : Mapping [ str , str ] | None = None ,
99
+ expand_iter : str | None = None ,
55
100
) -> None : ...
56
101
@overload
57
102
def unparse (
@@ -63,13 +108,15 @@ def unparse(
63
108
comment_key : str = "#comment" ,
64
109
* ,
65
110
attr_prefix : str = "@" ,
66
- cdata_key = "#text" ,
111
+ cdata_key : str = "#text" ,
67
112
depth : int = 0 ,
68
- preprocessor = None ,
113
+ # preprocessor is called like (preprocessor(key, value) for key, value in input_dict.items()).
114
+ # It is expected to return its input, or a modification thereof
115
+ preprocessor : Callable [[str , Any ], tuple [str , Any ]] | None = None ,
69
116
pretty : bool = False ,
70
117
newl : str = "\n " ,
71
118
indent : str | int = "\t " ,
72
119
namespace_separator : str = ":" ,
73
- namespaces = None ,
74
- expand_iter = None ,
120
+ namespaces : Mapping [ str , str ] | None = None ,
121
+ expand_iter : str | None = None ,
75
122
) -> str : ...
0 commit comments