Skip to content

Commit dcc330c

Browse files
authored
Change object inspection logic (#323)
Also adds a test for a generator function. Previously, different kinds of functions were not detected as the "function" condition was short-circuiting the cases. This is a first pass for allowing more details in this metadata. Addresses (but doesn't close) #301
2 parents 1786a76 + d7e3cd0 commit dcc330c

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

papyri/signature.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,22 @@ def __init__(self, target_item):
8787
self._sig = inspect.signature(target_item)
8888

8989
def to_node(self) -> SignatureNode:
90-
if inspect.isbuiltin(self.target_item):
91-
kind = "builtins"
92-
elif inspect.isfunction(self.target_item):
90+
kind = ""
91+
if inspect.isfunction(self.target_item):
9392
kind = "function"
94-
elif self.is_generator:
95-
kind = "generator"
96-
elif self.is_async_generator:
97-
kind = "async_generator"
98-
elif inspect.iscoroutinefunction(self.target_item):
99-
kind = "coroutine_function"
100-
else:
93+
if inspect.isbuiltin(self.target_item):
94+
kind = "built-in function"
95+
if inspect.isgeneratorfunction(self.target_item):
96+
kind = "generator function"
97+
if inspect.isasyncgenfunction(self.target_item):
98+
kind = "async_generator function"
99+
if inspect.iscoroutinefunction(self.target_item):
100+
kind = "coroutine function"
101+
if kind == "":
101102
assert False, f"Unknown kind for {self.target_item}"
103+
104+
# Why do we want to make sure this is not a coroutine?
105+
# What is special about a coroutine in this context?
102106
assert not inspect.iscoroutine(self.target_item)
103107

104108
parameters = []

papyri/tests/test_signatures.py

+72-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def async_function_2(posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwargs):
156156
@add
157157
def generator_function_3(posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwargs):
158158
"""{
159-
"kind": "function",
159+
"kind": "generator function",
160160
"parameters": [
161161
{
162162
"annotation": {
@@ -226,7 +226,7 @@ async def async_generator_function_4(
226226
posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwargs
227227
):
228228
"""{
229-
"kind": "function",
229+
"kind": "async_generator function",
230230
"parameters": [
231231
{
232232
"annotation": {
@@ -291,6 +291,76 @@ async def async_generator_function_4(
291291
yield
292292

293293

294+
@add
295+
async def coroutine_function_5(
296+
posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwargs
297+
):
298+
"""{
299+
"kind": "coroutine function",
300+
"parameters": [
301+
{
302+
"annotation": {
303+
"type": "Empty"
304+
},
305+
"default": {
306+
"type": "Empty"
307+
},
308+
"kind": "POSITIONAL_ONLY",
309+
"name": "posonly",
310+
"type": "ParameterNode"
311+
},
312+
{
313+
"annotation": {
314+
"type": "Empty"
315+
},
316+
"default": {
317+
"type": "Empty"
318+
},
319+
"kind": "POSITIONAL_OR_KEYWORD",
320+
"name": "pos_or_k",
321+
"type": "ParameterNode"
322+
},
323+
{
324+
"annotation": {
325+
"type": "Empty"
326+
},
327+
"default": {
328+
"data": "1",
329+
"type": "str"
330+
},
331+
"kind": "POSITIONAL_OR_KEYWORD",
332+
"name": "pos_ok_k_d",
333+
"type": "ParameterNode"
334+
},
335+
{
336+
"annotation": {
337+
"type": "Empty"
338+
},
339+
"default": {
340+
"type": "Empty"
341+
},
342+
"kind": "VAR_POSITIONAL",
343+
"name": "varargs",
344+
"type": "ParameterNode"
345+
},
346+
{
347+
"annotation": {
348+
"type": "Empty"
349+
},
350+
"default": {
351+
"type": "Empty"
352+
},
353+
"kind": "VAR_KEYWORD",
354+
"name": "varkwargs",
355+
"type": "ParameterNode"
356+
}
357+
],
358+
"return_annotation": {"type": "Empty"},
359+
"type": "SignatureNode"
360+
}"""
361+
pass
362+
363+
294364
@add
295365
def function_with_annotation5(a: int, b: Union[int, float]) -> Optional[bool]:
296366
"""

0 commit comments

Comments
 (0)