Skip to content

Commit 04a9bbf

Browse files
Merge pull request #752 from jarrodmillman/mypy
Add MyPy type checking
2 parents 2330236 + 51e25a5 commit 04a9bbf

File tree

103 files changed

+330
-214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+330
-214
lines changed

.pre-commit-config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,38 @@ repos:
3838
args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"]
3939
- id: ruff-format
4040

41+
- repo: https://github.com/pre-commit/mirrors-mypy
42+
rev: "v1.10.0"
43+
hooks:
44+
- id: mypy
45+
additional_dependencies:
46+
- types-aiofiles
47+
- types-requests
48+
- pandas-stubs
49+
- types-pillow
50+
- matplotlib
51+
exclude: |
52+
(?x)(
53+
^build/
54+
| ^pyximages/
55+
| conf\.py$
56+
| .*/setup.*\.py$
57+
| .*/demo.py$
58+
| .*/auto_examples/
59+
| advanced/mathematical_optimization/examples/plot_gradient_descent\.py$
60+
| advanced/mathematical_optimization/examples/helper/compare_optimizers\.py$
61+
| advanced/advanced_numpy/examples/view-colors\.py$
62+
| advanced/advanced_numpy/examples/stride-diagonals\.py$
63+
| advanced/interfacing_with_c/cython_numpy/test_cos_doubles\.py$
64+
| advanced/interfacing_with_c/numpy_shared/test_cos_doubles\.py$
65+
| advanced/interfacing_with_c/swig.*\.py$
66+
| advanced/advanced_numpy/examples/myobject_test\.py$
67+
| advanced/interfacing_with_c/numpy_shared/test_cos_doubles\.py$
68+
| advanced/interfacing_with_c/numpy_c_api/test_cos_module_np\.py$
69+
| intro/numpy/solutions/2_a_call_fortran\.py$
70+
| advanced/advanced_numpy/examples/mandelplot\.py$
71+
)
72+
4173
- repo: https://github.com/codespell-project/codespell
4274
rev: 193cd7d27cd571f79358af09a8fb8997e54f8fff # frozen: v2.3.0
4375
hooks:

advanced/advanced_numpy/examples/pilbuffer-answer.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,21 @@
77
"""
88

99
import numpy as np
10-
import Image
10+
from PIL import Image
1111

1212
# Let's make a sample image, RGBA format
1313

14-
x = np.zeros((200, 200, 4), dtype=np.int8)
14+
x = np.zeros((200, 200, 4), dtype=np.uint8)
1515

16-
x[:, :, 0] = 254 # red
16+
x[:, :, 0] = 255 # red
1717
x[:, :, 3] = 255 # opaque
1818

1919
data = x.view(np.int32) # Check that you understand why this is OK!
2020

2121
img = Image.frombuffer("RGBA", (200, 200), data)
2222
img.save("test.png")
2323

24-
#
2524
# Modify the original data, and save again.
26-
#
27-
# It turns out that PIL, which knows next to nothing about NumPy,
28-
# happily shares the same data.
29-
#
3025

31-
x[:, :, 1] = 254
26+
x[:, :, 1] = 255
3227
img.save("test2.png")

advanced/advanced_numpy/examples/pilbuffer.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
"""
2-
Exercise: using the buffer protocole
3-
====================================
2+
Exercise: using the buffer protocol
3+
===================================
44
5-
Skeletton of the code to do an exercise using the buffer protocole.
5+
Skeleton of the code to do an exercise using the buffer protocole.
66
"""
77

88
import numpy as np
9-
import Image
9+
from PIL import Image
1010

1111
# Let's make a sample image, RGBA format
1212

13-
x = np.zeros((200, 200, 4), dtype=np.int8)
13+
x = np.zeros((200, 200, 4), dtype=np.uint8)
1414

15-
# TODO: fill `x` with fully opaque red [255, 0, 0, 255]
15+
# TODO: fill `data` with fully opaque red [255, 0, 0, 255]
1616

17-
# TODO: RGBA images consist of 32-bit integers whose bytes are [RR,GG,BB,AA]
18-
# How to get that from ``x``?
17+
# TODO: `x` is an array of bytes (8-bit integers)
18+
# What we need for PIL to understand this data is RGBA array,
19+
# where each element is a 32-bit integer, with bytes [RR,GG,BB,AA].
20+
# How do we convert `x` to such an array, called `data`?
1921

2022
data = ...
2123

advanced/advanced_numpy/examples/plots/plot_maskedstats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import matplotlib.pyplot as plt
1111

1212
data = np.loadtxt("../../../../data/populations.txt")
13-
populations = np.ma.masked_array(data[:, 1:])
13+
populations = np.ma.masked_array(data[:, 1:]) # type: ignore[var-annotated]
1414
year = data[:, 0]
1515

1616
bad_years = ((year >= 1903) & (year <= 1910)) | ((year >= 1917) & (year <= 1918))

advanced/debugging/to_debug_solution.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ def compare_optimizers(optimizers):
5757
"""
5858
random_a = -1.3 + rng.random(size=100)
5959
random_b = 0.3 + rng.random(size=100)
60-
param_grid = product(FUNCTIONS, random_a, random_b)
61-
values = []
62-
for value in param_grid:
63-
values.append(value)
64-
param_grid = values
60+
param_grid = list(product(FUNCTIONS, random_a, random_b))
6561
print("Benching 1D root-finder optimizers from scipy.optimize:")
6662
for optimizer in OPTIMIZERS:
6763
print(

advanced/debugging/wiener_filtering.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def iterated_wiener(noisy_img, size=3):
4646
face = sp.datasets.face(gray=True)
4747
noisy_face = face + 20 * rng.integers(3, size=face.shape) - 30
4848

49-
plt.matshow(face[cut], cmap=plt.cm.gray)
50-
plt.matshow(noisy_face[cut], cmap=plt.cm.gray)
49+
plt.matshow(face[cut], cmap="gray")
50+
plt.matshow(noisy_face[cut], cmap="gray")
5151

5252
denoised_face = iterated_wiener(noisy_face)
53-
plt.matshow(denoised_face[cut], cmap=plt.cm.gray)
53+
plt.matshow(denoised_face[cut], cmap="gray")
5454

5555
plt.show()

advanced/image_processing/examples/plot_GMM.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
plt.text(0.57, 0.8, "histogram", fontsize=20, transform=plt.gca().transAxes)
4848
plt.yticks([])
4949
plt.subplot(133)
50-
plt.imshow(binary_img, cmap=plt.cm.gray, interpolation="nearest")
50+
plt.imshow(binary_img, cmap="gray", interpolation="nearest")
5151
plt.axis("off")
5252

5353
plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)

advanced/image_processing/examples/plot_block_mean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
block_mean.shape = (sx // 4, sy // 6)
2020

2121
plt.figure(figsize=(5, 5))
22-
plt.imshow(block_mean, cmap=plt.cm.gray)
22+
plt.imshow(block_mean, cmap="gray")
2323
plt.axis("off")
2424

2525
plt.show()

advanced/image_processing/examples/plot_blur.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616
plt.figure(figsize=(9, 3))
1717
plt.subplot(131)
18-
plt.imshow(blurred_face, cmap=plt.cm.gray)
18+
plt.imshow(blurred_face, cmap="gray")
1919
plt.axis("off")
2020
plt.subplot(132)
21-
plt.imshow(very_blurred, cmap=plt.cm.gray)
21+
plt.imshow(very_blurred, cmap="gray")
2222
plt.axis("off")
2323
plt.subplot(133)
24-
plt.imshow(local_mean, cmap=plt.cm.gray)
24+
plt.imshow(local_mean, cmap="gray")
2525
plt.axis("off")
2626

2727
plt.subplots_adjust(wspace=0, hspace=0.0, top=0.99, bottom=0.01, left=0.01, right=0.99)

advanced/image_processing/examples/plot_clean_morpho.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
l = 128
3737

3838
plt.subplot(141)
39-
plt.imshow(binary_img[:l, :l], cmap=plt.cm.gray)
39+
plt.imshow(binary_img[:l, :l], cmap="gray")
4040
plt.axis("off")
4141
plt.subplot(142)
42-
plt.imshow(open_img[:l, :l], cmap=plt.cm.gray)
42+
plt.imshow(open_img[:l, :l], cmap="gray")
4343
plt.axis("off")
4444
plt.subplot(143)
45-
plt.imshow(close_img[:l, :l], cmap=plt.cm.gray)
45+
plt.imshow(close_img[:l, :l], cmap="gray")
4646
plt.axis("off")
4747
plt.subplot(144)
48-
plt.imshow(mask[:l, :l], cmap=plt.cm.gray)
48+
plt.imshow(mask[:l, :l], cmap="gray")
4949
plt.contour(close_img[:l, :l], [0.5], linewidths=2, colors="r")
5050
plt.axis("off")
5151

0 commit comments

Comments
 (0)