Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bd25b4

Browse files
committed
Add mypy to pre-commit and some basic type hints for it to check
1 parent 104e06b commit 3bd25b4

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

.dmypy.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"pid": 7632, "connection_name": "\\\\.\\pipe\\dmypy-_CCjf3E3.pipe"}

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ repos:
1515
rev: v1.11.2
1616
hooks:
1717
- id: mypy
18-
args: [--strict, --ignore-missing-imports, "."]
18+
name: mypy
19+
entry: dmypy
20+
files: \.py$
21+
language: python
22+
require_serial: true
23+
args: ["run", "--", "--implicit-reexport", "--warn-unused-ignores", "--cache-fine-grained", "--ignore-missing-imports"]
1924
additional_dependencies: [tokenize-rt==6.0.0]

shapefile.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from urllib.parse import urlparse, urlunparse
2424
from urllib.request import urlopen, Request
2525

26+
from typing import Any, Union, Iterable, ByteString
27+
from collections.abc import Sequence
28+
2629
# Create named logger
2730
logger = logging.getLogger(__name__)
2831

@@ -89,7 +92,7 @@
8992
NODATA = -10e38 # as per the ESRI shapefile spec, only used for m-values.
9093

9194

92-
def b(v, encoding="utf-8", encodingErrors="strict"):
95+
def b(v: Any, encoding="utf-8", encodingErrors="strict") -> bytes:
9396
if isinstance(v, str):
9497
# For python 3 encode str to bytes.
9598
return v.encode(encoding, encodingErrors)
@@ -104,7 +107,9 @@ def b(v, encoding="utf-8", encodingErrors="strict"):
104107
return str(v).encode(encoding, encodingErrors)
105108

106109

107-
def u(v, encoding="utf-8", encodingErrors="strict"):
110+
def u(
111+
v: Union[bytes, str, None, int, ByteString], encoding="utf-8", encodingErrors="strict"
112+
) -> str:
108113
if isinstance(v, bytes):
109114
# For python 3 decode bytes to str.
110115
return v.decode(encoding, encodingErrors)
@@ -119,11 +124,11 @@ def u(v, encoding="utf-8", encodingErrors="strict"):
119124
return bytes(v).decode(encoding, encodingErrors)
120125

121126

122-
def is_string(v):
127+
def is_string(v: Any) -> bool:
123128
return isinstance(v, str)
124129

125130

126-
def pathlike_obj(path):
131+
def pathlike_obj(path: Any) -> Any:
127132
if isinstance(path, os.PathLike):
128133
return os.fsdecode(path)
129134
else:
@@ -140,8 +145,11 @@ class _Array(array.array):
140145
def __repr__(self):
141146
return str(self.tolist())
142147

148+
Point_T = Sequence[float]
149+
Coords_T = Sequence[Point_T]
150+
BBox_T = tuple[float, float, float, float]
143151

144-
def signed_area(coords, fast=False):
152+
def signed_area(coords: Coords_T, fast: bool = False) -> float:
145153
"""Return the signed area enclosed by a ring using the linear time
146154
algorithm. A value >= 0 indicates a counter-clockwise oriented ring.
147155
A faster version is possible by setting 'fast' to True, which returns
@@ -157,43 +165,47 @@ def signed_area(coords, fast=False):
157165
return area2 / 2.0
158166

159167

160-
def is_cw(coords):
168+
def is_cw(coords: Coords_T) -> bool:
161169
"""Returns True if a polygon ring has clockwise orientation, determined
162170
by a negatively signed area.
163171
"""
164172
area2 = signed_area(coords, fast=True)
165173
return area2 < 0
166174

167175

168-
def rewind(coords):
176+
def rewind(coords: Coords_T) -> list[Point_T]:
169177
"""Returns the input coords in reversed order."""
170178
return list(reversed(coords))
171179

172180

173-
def ring_bbox(coords):
181+
def ring_bbox(coords: Coords_T) -> BBox_T:
174182
"""Calculates and returns the bounding box of a ring."""
175183
xs, ys = zip(*coords)
176184
bbox = min(xs), min(ys), max(xs), max(ys)
177185
return bbox
178186

179187

180-
def bbox_overlap(bbox1, bbox2):
188+
def bbox_overlap(
189+
bbox1: BBox_T, bbox2: BBox_T
190+
) -> bool:
181191
"""Tests whether two bounding boxes overlap, returning a boolean"""
182192
xmin1, ymin1, xmax1, ymax1 = bbox1
183193
xmin2, ymin2, xmax2, ymax2 = bbox2
184194
overlap = xmin1 <= xmax2 and xmax1 >= xmin2 and ymin1 <= ymax2 and ymax1 >= ymin2
185195
return overlap
186196

187197

188-
def bbox_contains(bbox1, bbox2):
198+
def bbox_contains(
199+
bbox1: BBox_T, bbox2: BBox_T
200+
) -> bool:
189201
"""Tests whether bbox1 fully contains bbox2, returning a boolean"""
190202
xmin1, ymin1, xmax1, ymax1 = bbox1
191203
xmin2, ymin2, xmax2, ymax2 = bbox2
192204
contains = xmin1 < xmin2 and xmax1 > xmax2 and ymin1 < ymin2 and ymax1 > ymax2
193205
return contains
194206

195207

196-
def ring_contains_point(coords, p):
208+
def ring_contains_point(coords: Coords_T, p: Point_T) -> bool:
197209
"""Fast point-in-polygon crossings algorithm, MacMartin optimization.
198210
199211
Adapted from code by Eric Haynes
@@ -238,7 +250,9 @@ def ring_contains_point(coords, p):
238250
return inside_flag
239251

240252

241-
def ring_sample(coords, ccw=False):
253+
def ring_sample(
254+
coords: Coords_T, ccw: bool = False
255+
) -> Point_T:
242256
"""Return a sample point guaranteed to be within a ring, by efficiently
243257
finding the first centroid of a coordinate triplet whose orientation
244258
matches the orientation of the ring and passes the point-in-ring test.
@@ -286,7 +300,7 @@ def itercoords():
286300
raise Exception("Unexpected error: Unable to find a ring sample point.")
287301

288302

289-
def ring_contains_ring(coords1, coords2):
303+
def ring_contains_ring(coords1, coords2) -> bool:
290304
"""Returns True if all vertexes in coords2 are fully inside coords1."""
291305
return all((ring_contains_point(coords1, p2) for p2 in coords2))
292306

0 commit comments

Comments
 (0)