From 4c90b94c24e9e78382d12947fb27c203318d6c9c Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 3 Jun 2020 23:30:44 -0700 Subject: flake8-annotation linting Added some new annotations; need to finish more. --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- fatcat_scholar/api_entities.py | 12 +++++++++--- fatcat_scholar/search.py | 2 +- fatcat_scholar/transform.py | 6 +++--- fatcat_scholar/web.py | 14 +++++++------- tests/test_djvu_parse.py | 2 +- tests/test_scrub.py | 2 +- tests/test_transform.py | 4 ++-- 9 files changed, 34 insertions(+), 19 deletions(-) diff --git a/Pipfile b/Pipfile index 8ce8d81..2a4c9bf 100644 --- a/Pipfile +++ b/Pipfile @@ -37,6 +37,7 @@ ipython = "*" mypy = "*" pytype = "*" flake8 = "*" +flake8-annotations = "*" pytest = "*" pytest-cov = "*" diff --git a/Pipfile.lock b/Pipfile.lock index eae440e..6e903ea 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "583eee252ec72ba5420f90c0902fe0f44569f86b8183697ae8dfcab902b3c1a4" + "sha256": "52fe057c387f651778459f19e4080cbecfb7ab4d59e0fa789c5b1023e57035ef" }, "pipfile-spec": 6, "requires": { @@ -583,6 +583,14 @@ "index": "pypi", "version": "==3.8.2" }, + "flake8-annotations": { + "hashes": [ + "sha256:9091d920406a7ff10e401e0dd1baa396d1d7d2e3d101a9beecf815f5894ad554", + "sha256:f59fdceb8c8f380a20aed20e1ba8a57bde05935958166c52be2249f113f7ab75" + ], + "index": "pypi", + "version": "==2.1.0" + }, "importlab": { "hashes": [ "sha256:d855350d19dc10a17aabd2fe6f4b428ff1a936071f692fbf686a73694d26a51c" diff --git a/fatcat_scholar/api_entities.py b/fatcat_scholar/api_entities.py index df24eda..c4a4ace 100644 --- a/fatcat_scholar/api_entities.py +++ b/fatcat_scholar/api_entities.py @@ -1,11 +1,13 @@ import json import collections +from typing import Any, Optional + from fatcat_openapi_client import ApiClient _global_serde_api_client = ApiClient() -def entity_to_dict(entity, api_client=None): +def entity_to_dict(entity: Any, api_client: Optional[ApiClient] = None) -> dict: """ Hack to take advantage of the code-generated serialization code. @@ -20,7 +22,9 @@ def entity_to_dict(entity, api_client=None): return api_client.sanitize_for_serialization(entity) -def entity_from_json(json_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 @@ -33,6 +37,8 @@ def entity_from_json(json_str, entity_type, api_client=None): return api_client.deserialize(thing, entity_type) -def entity_from_dict(obj, entity_type, api_client=None): +def entity_from_dict( + obj: dict, entity_type: Any, api_client: Optional[ApiClient] = None +) -> Any: json_str = json.dumps(obj) return entity_from_json(json_str, entity_type, api_client=api_client) diff --git a/fatcat_scholar/search.py b/fatcat_scholar/search.py index 3d9ca9b..6402e2b 100644 --- a/fatcat_scholar/search.py +++ b/fatcat_scholar/search.py @@ -209,7 +209,7 @@ def do_fulltext_search( # Avoid deep paging problem. offset = deep_page_limit - search = search[offset:(offset+limit)] + search = search[offset : (offset + limit)] try: resp = search.execute() diff --git a/fatcat_scholar/transform.py b/fatcat_scholar/transform.py index 28c959b..3d47fb4 100644 --- a/fatcat_scholar/transform.py +++ b/fatcat_scholar/transform.py @@ -1,6 +1,6 @@ import sys import argparse -from typing import List, Dict, Optional, Any +from typing import List, Dict, Optional, Any, Sequence from fatcat_openapi_client import ReleaseEntity, FileEntity @@ -329,7 +329,7 @@ def transform_heavy(heavy: IntermediateBundle) -> Optional[ScholarDoc]: ) -def run_transform(infile): +def run_transform(infile: Sequence) -> None: for line in infile: obj = json.loads(line) @@ -350,7 +350,7 @@ def run_transform(infile): print(es_doc.json()) -def main(): +def main() -> None: """ Run this command like: diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py index 3f6982d..4e912d4 100644 --- a/fatcat_scholar/web.py +++ b/fatcat_scholar/web.py @@ -9,7 +9,7 @@ import babel.support from fastapi import FastAPI, APIRouter, Request, Depends from fastapi.staticfiles import StaticFiles from dynaconf import settings -from typing import Optional +from typing import Optional, List, Any from fatcat_scholar.hacks import Jinja2Templates from fatcat_scholar.search import do_fulltext_search, FulltextQuery, FulltextHits @@ -58,26 +58,26 @@ api = APIRouter() @api.get("/", operation_id="get_home") -async def home(): +async def home() -> Any: return {"endpoints": {"/": "this", "/search": "fulltext search"}} @api.get("/search", operation_id="get_search") -async def search(query: FulltextQuery = Depends(FulltextQuery)): +async def search(query: FulltextQuery = Depends(FulltextQuery)) -> Any: return {"message": "search results would go here, I guess"} web = APIRouter() -def locale_gettext(translations): +def locale_gettext(translations: List[Any]) -> Any: def gt(s): return translations.ugettext(s) return gt -def locale_ngettext(translations): +def locale_ngettext(translations: List[Any]) -> Any: def ngt(s, n): return translations.ungettext(s) @@ -144,7 +144,7 @@ async def web_about(request: Request, lang: LangPrefix = Depends(LangPrefix)): @web.get("/help", include_in_schema=False) -async def web_help(request: Request, lang: LangPrefix = Depends(LangPrefix)): +async def web_help(request: Request, lang: LangPrefix = Depends(LangPrefix)) -> Any: return i18n_templates[lang.code].TemplateResponse( "help.html", {"request": request, "locale": lang.code, "lang_prefix": lang.prefix}, @@ -157,7 +157,7 @@ async def web_search( query: FulltextQuery = Depends(FulltextQuery), lang: LangPrefix = Depends(LangPrefix), content: ContentNegotiation = Depends(ContentNegotiation), -): +) -> Any: if content.mimetype == "application/json": return await search(query) diff --git a/tests/test_djvu_parse.py b/tests/test_djvu_parse.py index 1f23de2..777f8bf 100644 --- a/tests/test_djvu_parse.py +++ b/tests/test_djvu_parse.py @@ -2,7 +2,7 @@ import io from fatcat_scholar.djvu import djvu_extract_leaf_texts -def test_djvu_extract_leaf_texts(): +def test_djvu_extract_leaf_texts() -> None: # https://archive.org/details/ERIC_ED441501 with open("tests/files/ERIC_ED441501_djvu.xml", "r") as f: diff --git a/tests/test_scrub.py b/tests/test_scrub.py index 692ed64..2cd68cc 100644 --- a/tests/test_scrub.py +++ b/tests/test_scrub.py @@ -1,7 +1,7 @@ from fatcat_scholar.schema import * -def test_scrub(): +def test_scrub() -> None: vectors = [ ( "“Please clean this piece… of text„", diff --git a/tests/test_transform.py b/tests/test_transform.py index 21efa51..e073591 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -4,7 +4,7 @@ from fatcat_scholar.schema import * from fatcat_scholar.api_entities import * -def test_es_release_from_release(): +def test_es_release_from_release() -> None: with open("tests/files/release_hsmo6p4smrganpb3fndaj2lon4.json", "r") as f: release = entity_from_json(f.read(), ReleaseEntity) @@ -17,7 +17,7 @@ def test_es_release_from_release(): assert obj.doi_prefix == "10.7717" -def test_es_biblio_from_release(): +def test_es_biblio_from_release() -> None: with open("tests/files/release_hsmo6p4smrganpb3fndaj2lon4.json", "r") as f: release = entity_from_json(f.read(), ReleaseEntity) -- cgit v1.2.3