Skip to content

Commit

Permalink
pre-commit: require PEP 563 type annotations
Browse files Browse the repository at this point in the history
Require `from __future__ import annotations` in every Python file that
isn't a known config file.  Prep for adding type annotations.

Signed-off-by: Benjamin Gilbert <[email protected]>
  • Loading branch information
bgilbert committed Sep 8, 2024
1 parent be71273 commit b16b405
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ repos:
hooks:
- id: check-hooks-apply
- id: check-useless-excludes

- repo: local
hooks:
- id: annotations
name: Require "from __future__ import annotations"
language: pygrep
types: [python]
# exclude config-like files
exclude: "^(setup\\.py|doc/conf\\.py|openslide/_version\\.py)$"
# Allow files with import statement, or of less than two characters.
# One-character files are allowed because that's the best we can do
# with paired negative lookbehind and lookahead assertions. ^ and $
# don't work because --multiline causes them to match at newlines.
entry: "(?<!.)(?!.)|\nfrom __future__ import annotations"
args: [--multiline, --negate]
2 changes: 2 additions & 0 deletions doc/jekyll_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# deployed to the website.
# Rename Sphinx output paths to drop the underscore.

from __future__ import annotations

import os

from sphinx.util import logging
Expand Down
2 changes: 2 additions & 0 deletions examples/deepzoom/deepzoom_multiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

from argparse import ArgumentParser
import base64
from collections import OrderedDict
Expand Down
2 changes: 2 additions & 0 deletions examples/deepzoom/deepzoom_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

from argparse import ArgumentParser
import base64
from io import BytesIO
Expand Down
2 changes: 2 additions & 0 deletions examples/deepzoom/deepzoom_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

"""An example program to generate a Deep Zoom directory tree from a slide."""

from __future__ import annotations

from argparse import ArgumentParser
import base64
from io import BytesIO
Expand Down
2 changes: 2 additions & 0 deletions openslide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
This package provides Python bindings for the OpenSlide library.
"""

from __future__ import annotations

from collections.abc import Mapping
from io import BytesIO

Expand Down
2 changes: 2 additions & 0 deletions openslide/deepzoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
OpenSlide objects.
"""

from __future__ import annotations

from io import BytesIO
import math
from xml.etree.ElementTree import Element, ElementTree, SubElement
Expand Down
2 changes: 2 additions & 0 deletions openslide/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
rather than in the high-level interface.)
"""

from __future__ import annotations

from ctypes import (
POINTER,
byref,
Expand Down
2 changes: 2 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

import os
from pathlib import Path

Expand Down
8 changes: 7 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

import ctypes
import unittest

Expand All @@ -35,13 +37,17 @@ def test_open_slide(self):
def test_lowlevel_available(self):
'''Ensure all exported functions have an 'available' attribute.'''
for name in dir(lowlevel):
attr = getattr(lowlevel, name)
# ignore classes and unexported functions
if name.startswith('_') or name[0].isupper():
continue
# ignore __future__ imports
if getattr(attr, '__module__', None) == '__future__':
continue
# ignore random imports
if hasattr(ctypes, name) or name in ('count', 'platform'):
continue
self.assertTrue(
hasattr(getattr(lowlevel, name), 'available'),
hasattr(attr, 'available'),
f'"{name}" missing "available" attribute',
)
2 changes: 2 additions & 0 deletions tests/test_deepzoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

import unittest

from common import file_path
Expand Down
2 changes: 2 additions & 0 deletions tests/test_imageslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

import unittest

from PIL import Image
Expand Down
2 changes: 2 additions & 0 deletions tests/test_openslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import annotations

from ctypes import ArgumentError
import re
import sys
Expand Down

0 comments on commit b16b405

Please sign in to comment.