Skip to content

Commit 6d469ff

Browse files
authored
Merge pull request #277 from bgilbert/future
pre-commit: require PEP 563 type annotations
2 parents be71273 + b16b405 commit 6d469ff

13 files changed

+44
-1
lines changed

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ repos:
6464
hooks:
6565
- id: check-hooks-apply
6666
- id: check-useless-excludes
67+
68+
- repo: local
69+
hooks:
70+
- id: annotations
71+
name: Require "from __future__ import annotations"
72+
language: pygrep
73+
types: [python]
74+
# exclude config-like files
75+
exclude: "^(setup\\.py|doc/conf\\.py|openslide/_version\\.py)$"
76+
# Allow files with import statement, or of less than two characters.
77+
# One-character files are allowed because that's the best we can do
78+
# with paired negative lookbehind and lookahead assertions. ^ and $
79+
# don't work because --multiline causes them to match at newlines.
80+
entry: "(?<!.)(?!.)|\nfrom __future__ import annotations"
81+
args: [--multiline, --negate]

doc/jekyll_fix.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
# deployed to the website.
2424
# Rename Sphinx output paths to drop the underscore.
2525

26+
from __future__ import annotations
27+
2628
import os
2729

2830
from sphinx.util import logging

examples/deepzoom/deepzoom_multiserver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
#
2121

22+
from __future__ import annotations
23+
2224
from argparse import ArgumentParser
2325
import base64
2426
from collections import OrderedDict

examples/deepzoom/deepzoom_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
#
2121

22+
from __future__ import annotations
23+
2224
from argparse import ArgumentParser
2325
import base64
2426
from io import BytesIO

examples/deepzoom/deepzoom_tile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

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

24+
from __future__ import annotations
25+
2426
from argparse import ArgumentParser
2527
import base64
2628
from io import BytesIO

openslide/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
This package provides Python bindings for the OpenSlide library.
2424
"""
2525

26+
from __future__ import annotations
27+
2628
from collections.abc import Mapping
2729
from io import BytesIO
2830

openslide/deepzoom.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
OpenSlide objects.
2424
"""
2525

26+
from __future__ import annotations
27+
2628
from io import BytesIO
2729
import math
2830
from xml.etree.ElementTree import Element, ElementTree, SubElement

openslide/lowlevel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
rather than in the high-level interface.)
3131
"""
3232

33+
from __future__ import annotations
34+
3335
from ctypes import (
3436
POINTER,
3537
byref,

tests/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
#
1919

20+
from __future__ import annotations
21+
2022
import os
2123
from pathlib import Path
2224

tests/test_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
#
1919

20+
from __future__ import annotations
21+
2022
import ctypes
2123
import unittest
2224

@@ -35,13 +37,17 @@ def test_open_slide(self):
3537
def test_lowlevel_available(self):
3638
'''Ensure all exported functions have an 'available' attribute.'''
3739
for name in dir(lowlevel):
40+
attr = getattr(lowlevel, name)
3841
# ignore classes and unexported functions
3942
if name.startswith('_') or name[0].isupper():
4043
continue
44+
# ignore __future__ imports
45+
if getattr(attr, '__module__', None) == '__future__':
46+
continue
4147
# ignore random imports
4248
if hasattr(ctypes, name) or name in ('count', 'platform'):
4349
continue
4450
self.assertTrue(
45-
hasattr(getattr(lowlevel, name), 'available'),
51+
hasattr(attr, 'available'),
4652
f'"{name}" missing "available" attribute',
4753
)

0 commit comments

Comments
 (0)