Skip to content

Commit

Permalink
fix mypy typechecks
Browse files Browse the repository at this point in the history
  • Loading branch information
jandom committed Jul 20, 2024
1 parent 8b08ab4 commit 0f9bb63
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .
pip install ".[tests]"
- name: Run mypy
run: |-
mypy --install-types --non-interactive qreader.py
29 changes: 17 additions & 12 deletions qreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@
("shift-jis", "big5") if os.name == "nt" else ("big5", "shift-jis")
)

CorrectionsType = typing.Literal["cropped_bbox", "corrected_perspective"]
FlavorType = typing.Literal["original", "inverted", "grayscale"]


@dataclass(frozen=True)
class DecodeQRResult:
scale_factor: float
corrections: typing.Literal["cropped_bbox", "corrected_perspective"]
flavor: typing.Literal["original", "inverted", "grayscale"]
corrections: CorrectionsType
flavor: FlavorType
blur_kernel_sizes: tuple[tuple[int, int]] | None
image: np.ndarray
result: Decoded


def wrap(
scale_factor: float,
corrections: typing.Literal["cropped_bbox", "corrected_perspective"],
flavor: typing.Literal["original", "inverted", "grayscale"],
corrections: CorrectionsType,
flavor: FlavorType,
blur_kernel_sizes: tuple[tuple[int, int], ...] | None,
image: np.ndarray,
results: typing.List[Decoded],
Expand Down Expand Up @@ -299,11 +302,13 @@ def _decode_qr_zbar(
image=cropped_quad, padded_quad_xy=updated_detection[PADDED_QUAD_XY]
)

corrections = {
"cropped_bbox": cropped_bbox,
"corrected_perspective": corrected_perspective,
}

for scale_factor in (1, 0.5, 2, 0.25, 3, 4):
for label, image in {
"cropped_bbox": cropped_bbox,
"corrected_perspective": corrected_perspective,
}.items():
for label, image in corrections.items():
# If rescaled_image will be larger than 1024px, skip it
# TODO: Decide a minimum size for the QRs based on the resize benchmark
if (
Expand All @@ -323,7 +328,7 @@ def _decode_qr_zbar(
if len(decodedQR) > 0:
return wrap(
scale_factor=scale_factor,
corrections=label,
corrections=typing.cast(CorrectionsType, label),
flavor="original",
blur_kernel_sizes=None,
image=rescaled_image,
Expand All @@ -335,7 +340,7 @@ def _decode_qr_zbar(
if len(decodedQR) > 0:
return wrap(
scale_factor=scale_factor,
corrections=label,
corrections=typing.cast(CorrectionsType, label),
flavor="inverted",
blur_kernel_sizes=None,
image=inverted_image,
Expand All @@ -356,7 +361,7 @@ def _decode_qr_zbar(
if len(decodedQR) > 0:
return wrap(
scale_factor=scale_factor,
corrections=label,
corrections=typing.cast(CorrectionsType, label),
flavor="grayscale",
blur_kernel_sizes=((5, 5), (7, 7)),
image=gray,
Expand All @@ -381,7 +386,7 @@ def _decode_qr_zbar(
if len(decodedQR) > 0:
return wrap(
scale_factor=scale_factor,
corrections=label,
corrections=typing.cast(CorrectionsType, label),
flavor="grayscale",
blur_kernel_sizes=((3, 3),),
image=sharpened_gray,
Expand Down
8 changes: 7 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[metadata]
description_file=README.md
license_files=LICENSE
license_files=LICENSE

[mypy]
[mypy-pyzbar.*]
ignore_missing_imports = True
[mypy-qrdet.*]
ignore_missing_imports = True
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"pyzbar",
"qrdet>=2.5",
],
extras_require={
"tests": ["mypy"],
},
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down

0 comments on commit 0f9bb63

Please sign in to comment.