From 6b35b2f918f06a36c67d5b36f7594bb26892e744 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 14 Jun 2024 15:13:24 +0200 Subject: [PATCH] chore: Fix strict pyright analysis errors --- papis_extract/annotation.py | 6 +++--- papis_extract/extractors/pdf.py | 12 ++++++++---- papis_extract/extractors/pocketbook.py | 22 ++++++++++++++++------ tests/test_annotation.py | 6 +++--- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/papis_extract/annotation.py b/papis_extract/annotation.py index 666c1bb..54b216d 100644 --- a/papis_extract/annotation.py +++ b/papis_extract/annotation.py @@ -1,6 +1,6 @@ import math from dataclasses import dataclass -from typing import Any, Optional +from typing import Any, Optional, cast import chevron import papis.config @@ -127,7 +127,7 @@ class Annotation: """ rawvalue: Any = papis.config.general_get(key, section=section) if isinstance(rawvalue, dict): - return rawvalue + return cast(dict[str, str], rawvalue) try: rawvalue = eval(rawvalue) except Exception: @@ -142,4 +142,4 @@ class Annotation: ) ) - return rawvalue + return cast(dict[str, str], rawvalue) diff --git a/papis_extract/extractors/pdf.py b/papis_extract/extractors/pdf.py index f61cefc..55c13f1 100644 --- a/papis_extract/extractors/pdf.py +++ b/papis_extract/extractors/pdf.py @@ -1,10 +1,12 @@ +# pyright: strict, reportMissingTypeStubs=false, reportUnknownMemberType=false from pathlib import Path +from typing import cast -import fitz import Levenshtein import magic import papis.config import papis.logging +import pymupdf as mu from papis_extract.annotation import Annotation from papis_extract.extractors import ExtractionError @@ -30,7 +32,9 @@ class PdfExtractor: annotations: list[Annotation] = [] try: with mu.Document(filename) as doc: - for page in doc: # pyright: ignore [reportUnknownVariableType] - missing stub + for ( + page + ) in doc: # pyright: ignore [reportUnknownVariableType] - missing stub page = cast(mu.Page, page) annot: mu.Annot for annot in page.annots(): @@ -61,7 +65,7 @@ class PdfExtractor: f"{'annotation' if len(annotations) == 1 else 'annotations'} for {filename}." ) - except mu.FileDataError as e: + except mu.FileDataError: raise ExtractionError return annotations @@ -82,7 +86,7 @@ class PdfExtractor: should both be included or are the same, using Levenshtein distance. """ - content = annotation.info["content"].replace("\n", " ") + content = cast(str, annotation.info["content"].replace("\n", " ")) written = page.get_textbox(annotation.rect).replace("\n", " ") # highlight with selection in note diff --git a/papis_extract/extractors/pocketbook.py b/papis_extract/extractors/pocketbook.py index 56ab17c..ad73ad7 100644 --- a/papis_extract/extractors/pocketbook.py +++ b/papis_extract/extractors/pocketbook.py @@ -1,3 +1,4 @@ +# pyright: strict, reportUnknownMemberType=false from pathlib import Path import magic @@ -42,9 +43,18 @@ class PocketBookExtractor: annotations: list[Annotation] = [] for bm in html.select("div.bookmark"): - content = (bm.select_one("div.bm-text>p") or html.new_string("")).text - note = (bm.select_one("div.bm-note>p") or html.new_string("")).text - page = (bm.select_one("p.bm-page") or html.new_string("")).text + content = str( + (bm.select_one("div.bm-text>p") or html.new_string("")).text + or "" # pyright: ignore [reportUnknownArgumentType] + ) + note = str( + (bm.select_one("div.bm-note>p") or html.new_string("")).text + or "" # pyright: ignore [reportUnknownArgumentType] + ) + page = int( + (bm.select_one("p.bm-page") or html.new_string("")).text + or 0 # pyright: ignore [reportUnknownArgumentType] + ) el_classes = bm.attrs.get("class", "").split(" ") color = (0, 0, 0) @@ -55,11 +65,11 @@ class PocketBookExtractor: a = Annotation( file=str(filename), - content=content or "", - note=note or "", + content=content, + note=note, color=color, type="Highlight", - page=int(page), + page=page, ) annotations.append(a) diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 98e7f31..ceb0c0e 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -18,7 +18,7 @@ from papis_extract.annotation import Annotation ), ], ) -def test_formatting_replacements(fmt_string, expected): +def test_formatting_replacements(fmt_string: str, expected: str): sut = Annotation( "myfile", content="I am the text value", @@ -36,7 +36,7 @@ def test_formatting_replacements(fmt_string, expected): ("{{quote}} ({{doc.author}})", "I am the text value (document-author)"), ], ) -def test_formatting_document_access(fmt_string, expected): +def test_formatting_document_access(fmt_string: str, expected: str): sut = Annotation( "myfile", content="I am the text value", @@ -64,7 +64,7 @@ def test_colorname_matches_exact(): (0.51, 0.0, 0.0), ], ) -def test_matches_inexact_colorname(color_value): +def test_matches_inexact_colorname(color_value: tuple[float, float, float]): sut = Annotation("testfile", color=color_value, minimum_similarity_color=0.833) c_name = sut.colorname assert c_name == "red"