Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass strict typing, py.typed and link issues #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading