chore: Fix strict pyright analysis errors

This commit is contained in:
Marty Oehme 2024-06-14 15:13:24 +02:00
parent 8093259551
commit 6b35b2f918
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
4 changed files with 30 additions and 16 deletions

View file

@ -1,6 +1,6 @@
import math import math
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Optional from typing import Any, Optional, cast
import chevron import chevron
import papis.config import papis.config
@ -127,7 +127,7 @@ class Annotation:
""" """
rawvalue: Any = papis.config.general_get(key, section=section) rawvalue: Any = papis.config.general_get(key, section=section)
if isinstance(rawvalue, dict): if isinstance(rawvalue, dict):
return rawvalue return cast(dict[str, str], rawvalue)
try: try:
rawvalue = eval(rawvalue) rawvalue = eval(rawvalue)
except Exception: except Exception:
@ -142,4 +142,4 @@ class Annotation:
) )
) )
return rawvalue return cast(dict[str, str], rawvalue)

View file

@ -1,10 +1,12 @@
# pyright: strict, reportMissingTypeStubs=false, reportUnknownMemberType=false
from pathlib import Path from pathlib import Path
from typing import cast
import fitz
import Levenshtein import Levenshtein
import magic import magic
import papis.config import papis.config
import papis.logging import papis.logging
import pymupdf as mu
from papis_extract.annotation import Annotation from papis_extract.annotation import Annotation
from papis_extract.extractors import ExtractionError from papis_extract.extractors import ExtractionError
@ -30,7 +32,9 @@ class PdfExtractor:
annotations: list[Annotation] = [] annotations: list[Annotation] = []
try: try:
with mu.Document(filename) as doc: 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) page = cast(mu.Page, page)
annot: mu.Annot annot: mu.Annot
for annot in page.annots(): for annot in page.annots():
@ -61,7 +65,7 @@ class PdfExtractor:
f"{'annotation' if len(annotations) == 1 else 'annotations'} for {filename}." f"{'annotation' if len(annotations) == 1 else 'annotations'} for {filename}."
) )
except mu.FileDataError as e: except mu.FileDataError:
raise ExtractionError raise ExtractionError
return annotations return annotations
@ -82,7 +86,7 @@ class PdfExtractor:
should both be included or are the same, using should both be included or are the same, using
Levenshtein distance. Levenshtein distance.
""" """
content = annotation.info["content"].replace("\n", " ") content = cast(str, annotation.info["content"].replace("\n", " "))
written = page.get_textbox(annotation.rect).replace("\n", " ") written = page.get_textbox(annotation.rect).replace("\n", " ")
# highlight with selection in note # highlight with selection in note

View file

@ -1,3 +1,4 @@
# pyright: strict, reportUnknownMemberType=false
from pathlib import Path from pathlib import Path
import magic import magic
@ -42,9 +43,18 @@ class PocketBookExtractor:
annotations: list[Annotation] = [] annotations: list[Annotation] = []
for bm in html.select("div.bookmark"): for bm in html.select("div.bookmark"):
content = (bm.select_one("div.bm-text>p") or html.new_string("")).text content = str(
note = (bm.select_one("div.bm-note>p") or html.new_string("")).text (bm.select_one("div.bm-text>p") or html.new_string("")).text
page = (bm.select_one("p.bm-page") 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(" ") el_classes = bm.attrs.get("class", "").split(" ")
color = (0, 0, 0) color = (0, 0, 0)
@ -55,11 +65,11 @@ class PocketBookExtractor:
a = Annotation( a = Annotation(
file=str(filename), file=str(filename),
content=content or "", content=content,
note=note or "", note=note,
color=color, color=color,
type="Highlight", type="Highlight",
page=int(page), page=page,
) )
annotations.append(a) annotations.append(a)

View file

@ -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( sut = Annotation(
"myfile", "myfile",
content="I am the text value", 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)"), ("{{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( sut = Annotation(
"myfile", "myfile",
content="I am the text value", content="I am the text value",
@ -64,7 +64,7 @@ def test_colorname_matches_exact():
(0.51, 0.0, 0.0), (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) sut = Annotation("testfile", color=color_value, minimum_similarity_color=0.833)
c_name = sut.colorname c_name = sut.colorname
assert c_name == "red" assert c_name == "red"