summaryrefslogtreecommitdiffstats
path: root/python/fatcat_tools/transforms/csl.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2021-11-03 12:29:39 -0700
committerBryan Newbold <bnewbold@robocracy.org>2021-11-03 12:31:07 -0700
commit10a2374051568edf3d872988e730328d899a0fdd (patch)
tree795be5e149a021f84bc4305c1811e63cc86f7aa1 /python/fatcat_tools/transforms/csl.py
parentcfab1ddcd8a05b62ecc16763d18a6ecee8fa234f (diff)
downloadfatcat-10a2374051568edf3d872988e730328d899a0fdd.tar.gz
fatcat-10a2374051568edf3d872988e730328d899a0fdd.zip
typing: first batch of python bulk type annotations
While these changes are more delicate than simple lint changes, this specific batch of edits and annotations was *relatively* simple, and resulted in few code changes other than function signature additions.
Diffstat (limited to 'python/fatcat_tools/transforms/csl.py')
-rw-r--r--python/fatcat_tools/transforms/csl.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/python/fatcat_tools/transforms/csl.py b/python/fatcat_tools/transforms/csl.py
index 2b39068a..03410ffb 100644
--- a/python/fatcat_tools/transforms/csl.py
+++ b/python/fatcat_tools/transforms/csl.py
@@ -1,4 +1,5 @@
import json
+from typing import Any, Dict, List
from citeproc import (
Citation,
@@ -9,20 +10,21 @@ from citeproc import (
)
from citeproc.source.json import CiteProcJSON
from citeproc_styles import get_style_filepath
+from fatcat_openapi_client import ReleaseContrib, ReleaseEntity
-def contribs_by_role(contribs, role):
+def contribs_by_role(contribs: List[ReleaseContrib], role: str) -> List[ReleaseContrib]:
ret = [c.copy() for c in contribs if c["role"] == role]
[c.pop("role") for c in ret]
# TODO: some note to self here
[c.pop("literal") for c in ret if "literal" in c]
if not ret:
- return None
+ return []
else:
return ret
-def release_to_csl(entity):
+def release_to_csl(entity: ReleaseEntity) -> Dict[str, Any]:
"""
Returns a python dict which can be json.dumps() to get a CSL-JSON (aka,
citeproc-JSON, aka Citation Style Language JSON)
@@ -188,9 +190,9 @@ def release_to_csl(entity):
return csl
-def refs_to_csl(entity):
+def refs_to_csl(entity: ReleaseEntity) -> List[Dict[str, Any]]:
ret = []
- for ref in entity.refs:
+ for ref in entity.refs or []:
if ref.release_id and False:
# TODO: fetch full entity from API and convert with release_to_csl
raise NotImplementedError
@@ -207,7 +209,7 @@ def refs_to_csl(entity):
return ret
-def citeproc_csl(csl_json, style, html=False):
+def citeproc_csl(csl_json: Dict[str, Any], style: str, html: bool = False) -> str:
"""
Renders a release entity to a styled citation.