Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Oct 2, 2023
1 parent 8516262 commit 832e7dc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
7 changes: 6 additions & 1 deletion papyri/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@
"""

from typing import Optional, Union

def example1(pos, only, /, var, args, *, kwargs, also=None):

def example1(
pos: int, only: None, /, var: Union[float, bool], args=1, *, kwargs, also=None
) -> Optional[str]:
"""
first example.
Expand All @@ -56,6 +60,7 @@ def example1(pos, only, /, var, args, *, kwargs, also=None):
... plt.show()
"""
return "ok"


def example2():
Expand Down
14 changes: 11 additions & 3 deletions papyri/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def to_parameter(self) -> inspect.Parameter:
class SignatureNode(Node):
kind: str # maybe enum, is it a function, async generator, generator, etc.
parameters: List[ParameterNode] # of pairs, we don't use dict because of ordering
return_annotation: Union[Empty, str]

def to_signature(self):
return inspect.Signature([p.to_parameter() for p in self.parameters])
Expand Down Expand Up @@ -101,6 +102,7 @@ def to_node(self) -> SignatureNode:

parameters = []
for param in self.parameters.values():
annotation: Union[Empty, str]
if param.annotation is inspect._empty:
annotation = _empty
elif isinstance(param.annotation, str):
Expand All @@ -119,7 +121,13 @@ def to_node(self) -> SignatureNode:
)
)
assert isinstance(kind, str)
return SignatureNode(kind=kind, parameters=parameters)
return SignatureNode(
kind=kind,
parameters=parameters,
return_annotation=_empty
if self._sig.return_annotation is inspect._empty
else str(self._sig.return_annotation),
)

@property
def parameters(self):
Expand All @@ -145,10 +153,10 @@ def annotations(self) -> bool:
return self.target_item.__annotations__

@property
def return_annotation(self) -> Optional[str]:
def return_annotation(self) -> Union[Empty, str]:
return_annotation = self._sig.return_annotation
return (
None
_empty
if return_annotation is inspect._empty
else inspect.formatannotation(return_annotation)
)
Expand Down
1 change: 1 addition & 0 deletions papyri/tests/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,6 @@ def test_self():
"default": {"data": "None", "type": "str"},
},
],
"return_annotation": {"type": "Empty"},
}
assert g.data["papyri"].to_dict()["signature"] is None
47 changes: 47 additions & 0 deletions papyri/tests/test_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from papyri.signature import Signature as SignatureObject, SignatureNode
import json
from typing import Union, Optional

import pytest

Expand Down Expand Up @@ -78,6 +79,7 @@ def function_1(posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwargs):
"type": "ParameterNode"
}
],
"return_annotation": {"type": "Empty"},
"type": "SignatureNode"
}"""
pass
Expand Down Expand Up @@ -145,6 +147,7 @@ def async_function_2(posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwargs):
"type": "ParameterNode"
}
],
"return_annotation": {"type": "Empty"},
"type": "SignatureNode"
}"""
pass
Expand Down Expand Up @@ -212,6 +215,7 @@ def generator_function_3(posonly, /, pos_or_k, pos_ok_k_d=1, *varargs, **varkwar
"type": "ParameterNode"
}
],
"return_annotation": {"type": "Empty"},
"type": "SignatureNode"
}"""
yield
Expand Down Expand Up @@ -281,11 +285,54 @@ async def async_generator_function_4(
"type": "ParameterNode"
}
],
"return_annotation": {"type": "Empty"},
"type": "SignatureNode"
}"""
yield


@add
def function_with_annotation5(a: int, b: Union[int, float]) -> Optional[bool]:
"""
{
"kind": "function",
"parameters": [
{
"annotation": {
"data": "int",
"type": "str"
},
"default": {
"type": "Empty"
},
"kind": "POSITIONAL_OR_KEYWORD",
"name": "a",
"type": "ParameterNode"
},
{
"annotation": {
"data": "Union[int, float]",
"type": "str"
},
"default": {
"type": "Empty"
},
"kind": "POSITIONAL_OR_KEYWORD",
"name": "b",
"type": "ParameterNode"
}
],
"return_annotation": {
"data": "typing.Optional[bool]",
"type": "str"
},
"type": "SignatureNode"
}
"""
pass


@pytest.mark.parametrize(
"func",
all_funcs,
Expand Down

0 comments on commit 832e7dc

Please sign in to comment.