Skip to content

Commit c6e513c

Browse files
committed
Handle type annotations in function signatures
1 parent 9bbb25d commit c6e513c

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

bpython/inspection.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import pydoc
2727
import re
2828
from collections import namedtuple
29-
from typing import Any, Optional, Type
29+
from typing import Any, Optional, Type, Dict, List
3030
from types import MemberDescriptorType, TracebackType
3131
from ._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}

bpython/test/test_inspection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ def test_getfuncprops_numpy_array(self):
147147
# np.array(object, dtype=None, *, ...).
148148
self.assertEqual(props.argspec.args, ["object", "dtype"])
149149

150-
@unittest.expectedFailure
151150
def test_issue_966_freestanding(self):
152151
def fun(number, lst=[]):
153152
"""

0 commit comments

Comments
 (0)