2626import pydoc
2727import re
2828from collections import namedtuple
29- from typing import Any , Optional , Type
29+ from typing import Any , Optional , Type , Dict , List
3030from types import MemberDescriptorType , TracebackType
3131from ._typing_compat import Literal
3232
@@ -121,15 +121,16 @@ def __repr__(self):
121121 __str__ = __repr__
122122
123123
124- def parsekeywordpairs (signature ):
125- tokens = Python3Lexer ().get_tokens (signature )
124+ def parsekeywordpairs (signature : str ) -> Dict [str , str ]:
126125 preamble = True
127126 stack = []
128- substack = []
127+ substack : List [ str ] = []
129128 parendepth = 0
130- for token , value in tokens :
129+ annotation = False
130+ for token , value in Python3Lexer ().get_tokens (signature ):
131131 if preamble :
132132 if token is Token .Punctuation and value == "(" :
133+ # First "(" starts the list of arguments
133134 preamble = False
134135 continue
135136
@@ -141,14 +142,23 @@ def parsekeywordpairs(signature):
141142 elif value == ":" and parendepth == - 1 :
142143 # End of signature reached
143144 break
145+ elif value == ":" and parendepth == 0 :
146+ # Start of type annotation
147+ annotation = True
148+
144149 if (value == "," and parendepth == 0 ) or (
145150 value == ")" and parendepth == - 1
146151 ):
147152 stack .append (substack )
148153 substack = []
154+ # If type annotation didn't end before, ti does now.
155+ annotation = False
149156 continue
157+ elif token is Token .Operator and value == "=" and parendepth == 0 :
158+ # End of type annotation
159+ annotation = False
150160
151- if value and (parendepth > 0 or value .strip ()):
161+ if value and not annotation and (parendepth > 0 or value .strip ()):
152162 substack .append (value )
153163
154164 return {item [0 ]: "" .join (item [2 :]) for item in stack if len (item ) >= 3 }
0 commit comments