Skip to content

Commit

Permalink
Pass strict typing and link issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Sep 18, 2024
1 parent e1cd9ee commit 75d2913
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@
extensions += ['sphinx.ext.extlinks']

# local

nitpick_ignore += [
# jaraco/jaraco.imaging#3
('py:class', 'PIL.Image.Image'),
]
30 changes: 20 additions & 10 deletions jaraco/imaging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,41 @@
import io
import operator
import struct
from collections import namedtuple
from typing import Iterable, NamedTuple, Tuple, Union

import PIL.Image
from importlib_resources import files

import jaraco.clipboard


def calc_aspect(size):
def calc_aspect(size: Iterable[int]) -> float:
"aspect = size[0] / size[1] # width/height"
return functools.reduce(operator.truediv, size)


Dimensions = namedtuple('Dimensions', 'width height')
class Dimensions(NamedTuple):
width: int
height: int


def replace_height(size, new_height):
def replace_height(size: Dimensions, new_height: int) -> Dimensions:
return Dimensions(size.width, new_height)


def replace_width(size, new_width):
def replace_width(size: Dimensions, new_width: int) -> Dimensions:
return Dimensions(new_width, size.height)


def resize_with_aspect(image, max_size, *args, **kargs):
_PILImageParams = Union[int, Tuple[float, float, float, float], float, None]


def resize_with_aspect(
image: PIL.Image.Image,
max_size: Iterable[int],
*args: _PILImageParams,
**kargs: _PILImageParams,
) -> PIL.Image.Image:
"""
Resizes a PIL image to a maximum size specified while maintaining
the aspect ratio of the image.
Expand All @@ -55,15 +65,15 @@ def resize_with_aspect(image, max_size, *args, **kargs):
# width is the limiting factor
new_width = int(round(max_size.height * aspect))
new_size = replace_width(max_size, new_width)
return image.resize(new_size, *args, **kargs)
return image.resize(new_size, *args, **kargs) # type: ignore[arg-type] # Assume the user passed correct parameters or let it fail


def load_apng():
def load_apng() -> PIL.Image.Image:
apng = files() / 'sample.png'
return PIL.Image.open(io.BytesIO(apng.read_bytes()))


def get_image():
def get_image() -> PIL.Image.Image:
"""
Stolen from lpaste. TODO: extract to jaraco.clipboard or similar.
"""
Expand All @@ -75,7 +85,7 @@ def get_image():
return PIL.Image.open(img_stream)


def save_clipboard_image():
def save_clipboard_image() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('filename')
filename = parser.parse_args().filename
Expand Down
Empty file added jaraco/imaging/py.typed
Empty file.
6 changes: 5 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[mypy]
# Is the project well-typed?
strict = False
strict = True

# Early opt-in even when strict = False
warn_unused_ignores = True
Expand All @@ -13,3 +13,7 @@ explicit_package_bases = True
disable_error_code =
# Disable due to many false positives
overload-overlap,

# jaraco/jaraco.clipboard#15
[mypy-jaraco.clipboard.*]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions newsfragments/3.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Complete annotations and add ``py.typed`` marker -- by :user:`Avasam`
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,3 @@ save-cb-image = "jaraco.imaging:save_clipboard_image"


[tool.setuptools_scm]


[tool.pytest-enabler.mypy]
# Disabled due to jaraco/skeleton#143

0 comments on commit 75d2913

Please sign in to comment.