Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6e513c

Browse files
committed
Handle type annotations in function signatures
1 parent9bbb25d commitc6e513c

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
importpydoc
2727
importre
2828
fromcollectionsimportnamedtuple
29-
fromtypingimportAny,Optional,Type
29+
fromtypingimportAny,Optional,Type,Dict,List
3030
fromtypesimportMemberDescriptorType,TracebackType
3131
from ._typing_compatimportLiteral
3232

@@ -121,15 +121,16 @@ def __repr__(self):
121121
__str__=__repr__
122122

123123

124-
defparsekeywordpairs(signature):
125-
tokens=Python3Lexer().get_tokens(signature)
124+
defparsekeywordpairs(signature:str)->Dict[str,str]:
126125
preamble=True
127126
stack= []
128-
substack= []
127+
substack:List[str]= []
129128
parendepth=0
130-
fortoken,valueintokens:
129+
annotation=False
130+
fortoken,valueinPython3Lexer().get_tokens(signature):
131131
ifpreamble:
132132
iftokenisToken.Punctuationandvalue=="(":
133+
# First "(" starts the list of arguments
133134
preamble=False
134135
continue
135136

@@ -141,14 +142,23 @@ def parsekeywordpairs(signature):
141142
elifvalue==":"andparendepth==-1:
142143
# End of signature reached
143144
break
145+
elifvalue==":"andparendepth==0:
146+
# Start of type annotation
147+
annotation=True
148+
144149
if (value==","andparendepth==0)or (
145150
value==")"andparendepth==-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+
eliftokenisToken.Operatorandvalue=="="andparendepth==0:
158+
# End of type annotation
159+
annotation=False
150160

151-
ifvalueand (parendepth>0orvalue.strip()):
161+
ifvalueandnotannotationand(parendepth>0orvalue.strip()):
152162
substack.append(value)
153163

154164
return {item[0]:"".join(item[2:])foriteminstackiflen(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
deftest_issue_966_freestanding(self):
152151
deffun(number,lst=[]):
153152
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp