26
26
import pydoc
27
27
import re
28
28
from collections import namedtuple
29
- from typing import Any , Optional , Type
29
+ from typing import Any , Optional , Type , Dict , List
30
30
from types import MemberDescriptorType , TracebackType
31
31
from ._typing_compat import Literal
32
32
@@ -121,15 +121,16 @@ def __repr__(self):
121
121
__str__ = __repr__
122
122
123
123
124
- def parsekeywordpairs (signature ):
125
- tokens = Python3Lexer ().get_tokens (signature )
124
+ def parsekeywordpairs (signature : str ) -> Dict [str , str ]:
126
125
preamble = True
127
126
stack = []
128
- substack = []
127
+ substack : List [ str ] = []
129
128
parendepth = 0
130
- for token , value in tokens :
129
+ annotation = False
130
+ for token , value in Python3Lexer ().get_tokens (signature ):
131
131
if preamble :
132
132
if token is Token .Punctuation and value == "(" :
133
+ # First "(" starts the list of arguments
133
134
preamble = False
134
135
continue
135
136
@@ -141,14 +142,23 @@ def parsekeywordpairs(signature):
141
142
elif value == ":" and parendepth == - 1 :
142
143
# End of signature reached
143
144
break
145
+ elif value == ":" and parendepth == 0 :
146
+ # Start of type annotation
147
+ annotation = True
148
+
144
149
if (value == "," and parendepth == 0 ) or (
145
150
value == ")" and parendepth == - 1
146
151
):
147
152
stack .append (substack )
148
153
substack = []
154
+ # If type annotation didn't end before, ti does now.
155
+ annotation = False
149
156
continue
157
+ elif token is Token .Operator and value == "=" and parendepth == 0 :
158
+ # End of type annotation
159
+ annotation = False
150
160
151
- if value and (parendepth > 0 or value .strip ()):
161
+ if value and not annotation and (parendepth > 0 or value .strip ()):
152
162
substack .append (value )
153
163
154
164
return {item [0 ]: "" .join (item [2 :]) for item in stack if len (item ) >= 3 }
0 commit comments