From 42c38edc9966267d591ea314a201757fa07f88a2 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 2 Nov 2021 16:55:03 -0700 Subject: entity transforms: add basic type annotations --- python/fatcat_tools/transforms/entities.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'python') diff --git a/python/fatcat_tools/transforms/entities.py b/python/fatcat_tools/transforms/entities.py index 3892d54a..799d5d6c 100644 --- a/python/fatcat_tools/transforms/entities.py +++ b/python/fatcat_tools/transforms/entities.py @@ -1,10 +1,12 @@ - +import collections import json +from typing import Any, Dict, List, Optional + import toml -import collections from fatcat_openapi_client import ApiClient -def entity_to_dict(entity, api_client=None) -> dict: + +def entity_to_dict(entity: Any, api_client: Optional[ApiClient] = None) -> Dict[str, Any]: """ Hack to take advantage of the code-generated serialization code. @@ -18,7 +20,10 @@ def entity_to_dict(entity, api_client=None) -> dict: api_client = ApiClient() return api_client.sanitize_for_serialization(entity) -def entity_from_json(json_str: str, entity_type, api_client=None): + +def entity_from_json( + json_str: str, entity_type: Any, api_client: Optional[ApiClient] = None +) -> Any: """ Hack to take advantage of the code-generated deserialization code @@ -26,15 +31,19 @@ def entity_from_json(json_str: str, entity_type, api_client=None): """ if not api_client: api_client = ApiClient() - thing = collections.namedtuple('Thing', ['data']) + thing = collections.namedtuple("Thing", ["data"]) thing.data = json_str return api_client.deserialize(thing, entity_type) + def entity_from_dict(obj: dict, entity_type, api_client=None): json_str = json.dumps(obj) return entity_from_json(json_str, entity_type, api_client=api_client) -def entity_to_toml(entity, api_client=None, pop_fields=None) -> str: + +def entity_to_toml( + entity: Any, api_client: Optional[ApiClient] = None, pop_fields: Optional[List[str]] = None +) -> str: """ pop_fields parameter can be used to strip out some fields from the resulting TOML. Eg, for fields which should not be edited, like the ident. @@ -45,6 +54,9 @@ def entity_to_toml(entity, api_client=None, pop_fields=None) -> str: obj.pop(k, None) return toml.dumps(obj) -def entity_from_toml(toml_str: str, entity_type, api_client=None): + +def entity_from_toml( + toml_str: str, entity_type: Any, api_client: Optional[List[str]] = None +) -> Any: obj = toml.loads(toml_str) return entity_from_dict(obj, entity_type, api_client=api_client) -- cgit v1.2.3