Skip to content

Commit

Permalink
Start threading Structured signature into existing datastructures.
Browse files Browse the repository at this point in the history
There are many objects that are called "Signature", Just a draft to try
to rename the current one to "TextSignature", to differentiate, and try
to pass the current one to be serialized/loaded.
  • Loading branch information
Carreau committed Sep 27, 2023
1 parent 88c43bc commit 6a7616b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 45 deletions.
6 changes: 3 additions & 3 deletions papyri/crosslink.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Fig,
Section,
SeeAlsoItem,
Signature,
TextSignature,
encoder,
TocTree,
)
Expand Down Expand Up @@ -72,7 +72,7 @@ class IngestedBlobs(Node):
"aliases",
"example_section_data",
"see_also",
"signature",
"textsignature",
"references",
"logo",
"qa",
Expand All @@ -87,7 +87,7 @@ class IngestedBlobs(Node):
aliases: List[str]
example_section_data: Section
see_also: List[SeeAlsoItem] # see also data
signature: Signature
textsignature: TextSignature
references: Optional[List[str]]
qa: str
arbitrary: List[Section]
Expand Down
82 changes: 44 additions & 38 deletions papyri/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
RefInfo,
Section,
SeeAlsoItem,
Signature,
TextSignature,
parse_rst_section,
)
from .toc import make_tree
Expand Down Expand Up @@ -536,7 +536,7 @@ def gen_main(
g = Gen(dummy_progress=dummy_progress, config=config)
g.log.info("Will write data to %s", target_dir)
if debug:
g.log.setLevel("DEBUG")
g.log.setLevel(logging.DEBUG)
g.log.debug("Log level set to debug")

g.collect_package_metadata(
Expand Down Expand Up @@ -730,6 +730,20 @@ class DocBlob(Node):
as well as link to external references, like images generated.
"""

__slots__ = (
"content",
"example_section_data",
"ordered_sections",
"item_file",
"item_line",
"item_type",
"aliases",
"see_also",
"textsignature",
"references",
"arbitrary",
)

@classmethod
def _deserialise(cls, **kwargs):
# print_("will deserialise", cls)
Expand Down Expand Up @@ -769,7 +783,7 @@ def _deserialise(cls, **kwargs):
item_type: Optional[str]
aliases: List[str]
see_also: List[SeeAlsoItem] # see also data
signature: Signature
textsignature: TextSignature
references: Optional[List[str]]
arbitrary: List[Section]

Expand All @@ -784,38 +798,17 @@ def slots(self):
"item_file",
"item_line",
"item_type",
"signature",
"textsignature",
"references",
"aliases",
"arbitrary",
]

@classmethod
def new(cls):
return cls({}, None, None, None, None, None, [], [], Signature(None), None, [])

# def __init__(
# self,
# content,
# example_section_data,
# ordered_sections,
# item_file,
# item_line,
# item_type,
# aliases,
# see_also,
# signature,
# references,
# arbitrary,
# ):
# self.content = content
# self.example_section_data = example_section_data
# self.ordered_sections = ordered_sections
# self.item_file = item_file
# self.item_line = item_line
# self.item_type = item_type
# self.aliases = aliases
# self.signature = signature
return cls(
{}, None, None, None, None, None, [], [], TextSignature(None), None, []
)


def _numpy_data_to_section(data: List[Tuple[str, str, List[str]]], title: str, qa):
Expand Down Expand Up @@ -868,14 +861,25 @@ class APIObjectInfo:

kind: str
docstring: str
signature: Optional[Signature]
signature: Optional[ObjectSignature]
name: str

def __init__(self, kind, docstring, signature, name, qa):
def __repr__(self):
return f"<APIObject {self.kind=} {self.docstring=} self.signature={str(self.signature)} {self.name=}>"

def __init__(
self,
kind: str,
docstring: str,
signature: Optional[ObjectSignature],
name: str,
qa: str,
):
assert isinstance(signature, (ObjectSignature, type(None)))
self.kind = kind
self.name = name
self.docstring = docstring
self.parsed = []
self.parsed: List[Any] = []
self.signature = signature
self._qa = qa

Expand Down Expand Up @@ -1347,7 +1351,7 @@ def collect_narrative_docs(self):
blob.aliases = []
blob.example_section_data = Section([], None)
blob.see_also = []
blob.signature = Signature(None)
blob.textsignature = TextSignature(None)
blob.references = None
blob.validate()
titles = [s.title for s in blob.arbitrary if s.title]
Expand Down Expand Up @@ -1533,13 +1537,14 @@ def prepare_doc_for_one_object(

item_type = str(type(target_item))
if blob.content["Signature"]:
blob.signature = Signature(blob.content.pop("Signature"))
blob.textsignature = TextSignature(blob.content.pop("Signature"))
else:
assert blob is not None
assert api_object is not None

blob.signature = Signature(api_object.signature)
blob.textsignature = TextSignature(str(api_object.signature))
del blob.content["Signature"]
self.log.debug("%r", blob.textsignature)

if api_object.special("Examples"):
# warnings this is true only for non-modules
Expand Down Expand Up @@ -1814,11 +1819,9 @@ def helper_1(
"module", item_docstring, None, target_item.__name__, qa
)
elif isinstance(target_item, (FunctionType, builtin_function_or_method)):
sig: Optional[str]
sig: Optional[ObjectSignature]
try:
sig = str(ObjectSignature(target_item))
# sig = qa.split(":")[-1] + sig
# sig = re.sub("at 0x[0-9a-f]+", "at 0x0000000", sig)
sig = ObjectSignature(target_item)
except (ValueError, TypeError):
sig = None
try:
Expand Down Expand Up @@ -1957,6 +1960,7 @@ def collect_api_docs(self, root: str, limit_to: List[str]):
qa=qa,
target_item=target_item,
)
self.log.debug("%r", api_object)
if ecollector.errored:
if ecollector._errors.keys():
self.log.warning(
Expand Down Expand Up @@ -2086,6 +2090,8 @@ def collect_api_docs(self, root: str, limit_to: List[str]):
doc_blob.validate()
except Exception as e:
raise type(e)(f"Error in {qa}")
self.log.debug(doc_blob.textsignature)
self.log.debug(doc_blob.to_dict())
self.put(qa, doc_blob)
if figs:
self.log.debug("Found %s figures", len(figs))
Expand Down
6 changes: 3 additions & 3 deletions papyri/html.tpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@



{% if doc.signature.value -%}
<code class='signature'>{{doc.signature.value}}</code>
{% if doc.textsignature.value -%}
<code class='signature'>{{name}}{{doc.textsignature.value}}</code>
{%- endif -%}

{%- for section in doc.content.keys() if doc.content[section] -%}

{% if (section not in ['Extended Summary','Signature', 'Summary']) and len(doc.content[section]) > 0 -%}
{% if (section not in ['Extended Summary','TextSignature', 'Summary']) and len(doc.content[section]) > 0 -%}
<h3 id="section-{{section}}">{{section}}</h3>
{% endif -%}
{% if section in ['Extended Summary', 'Summary', 'Notes'] -%}
Expand Down
1 change: 1 addition & 0 deletions papyri/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ def render_one(
qa=qa,
version=meta["version"],
module=qa.split(".")[0],
name=qa.split(":")[-1].split(".")[-1],
backrefs=backrefs,
parts=parts,
parts_links=parts_links,
Expand Down
2 changes: 1 addition & 1 deletion papyri/take2.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def from_untrusted(cls, module, version, kind, path):


@register(4011)
class Signature(Node):
class TextSignature(Node):
value: Optional[str]


Expand Down
13 changes: 13 additions & 0 deletions papyri/tests/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ def test_numpy(module, submodules, objects):

for o in objects:
assert (td / "module" / f"{o}.json").exists()


def test_self():
from papyri.gen import Gen, Config

c = Config(dry_run=True, dummy_progress=True)
g = Gen(False, config=c)
g.collect_package_metadata("papyri", ".", {})
g.collect_api_docs("papyri", {"papyri.examples:example1"})
assert g.data["papyri.examples:example1"].to_dict()["textsignature"] == {
"type": "TextSignature",
"value": "(pos, only, /, var, args, *, kwargs, also=None)",
}

0 comments on commit 6a7616b

Please sign in to comment.