Skip to content

Commit eb725e7

Browse files
authored
Assorted cleanup + linting (#260)
* Switch to extend per perflint suggestion * Improve return hygiene * Cleanup tests * Add check on import conventions and raises
1 parent 972a0e6 commit eb725e7

File tree

16 files changed

+100
-92
lines changed

16 files changed

+100
-92
lines changed

pyproject.toml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,29 @@ format.indent-style = "space"
9292
format.quote-style = "single"
9393
format.docstring-code-format = true
9494
lint.select = [
95-
"ANN", # flake8-annotations
96-
"B", # flake8-bugbear
97-
"C4", # flake8-comprehensions
98-
"E", # pycodestyle error
99-
"ERA", # eradicate (commented out code)
100-
"F", # pyflakes
101-
"FA", # flake8-future-annotations
102-
"I", # isort
103-
"N", # pep8-naming
104-
"NPY", # numpy
105-
"PD", # pandas-vet
106-
"PTH", # flake8-use-pathlib
107-
"RUF", # ruff-specific rules
108-
"SIM", # flake8-simplify
109-
"TC", # flake8-type-checking (performance improvements)
110-
"TRY", # tryceratops
111-
"UP", # pyupgrade
112-
"W", # pycodestyle warning
95+
"ANN", # flake8-annotations
96+
"B", # flake8-bugbear
97+
"C4", # flake8-comprehensions
98+
"E", # pycodestyle error
99+
"ERA", # eradicate (commented out code)
100+
"F", # pyflakes
101+
"FA", # flake8-future-annotations
102+
"I", # isort
103+
"ICN", # flake8-import-conventions
104+
"N", # pep8-naming
105+
"NPY", # numpy
106+
"PD", # pandas-vet
107+
"PERF", # perflint
108+
"PT", # flake8-pytest-style
109+
"PTH", # flake8-use-pathlib
110+
"RET", # flake8-return
111+
"RSE", # flake8-raise
112+
"RUF", # ruff-specific rules
113+
"SIM", # flake8-simplify
114+
"TC", # flake8-type-checking (performance improvements)
115+
"TRY", # tryceratops
116+
"UP", # pyupgrade
117+
"W", # pycodestyle warning
113118
]
114119
lint.ignore = [
115120
"E501", # line-too-long

src/data_morph/plotting/animation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ def ease_in_out_quadratic(step: int | float) -> int | float:
179179
"""
180180
if step < 0.5:
181181
return 2 * step**2
182-
else:
183-
step = step * 2 - 1
184-
return -0.5 * (step * (step - 2) - 1)
182+
step = step * 2 - 1
183+
return -0.5 * (step * (step - 2) - 1)
185184

186185

187186
@check_step

src/data_morph/plotting/static.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@ def plot(
116116
dirname.mkdir(parents=True, exist_ok=True)
117117

118118
fig.savefig(save_to, bbox_inches='tight', **save_kwds)
119-
plt.close(fig)
119+
return plt.close(fig)

src/data_morph/shapes/bases/line_collection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class LineCollection(Shape):
3131
def __init__(self, *lines: Iterable[Iterable[Number]]) -> None:
3232
# check that lines with the same starting and ending points raise an error
3333
for line in lines:
34-
start, end = line
35-
if np.allclose(start, end):
34+
if np.allclose(*line):
3635
raise ValueError(f'Line {line} has the same start and end point')
3736

3837
self.lines = np.array(lines)

src/data_morph/shapes/points/scatter.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ def __init__(self, dataset: Dataset) -> None:
3333
center = (dataset.data.x.mean(), dataset.data.y.mean())
3434
points = [center]
3535
max_radius = max(dataset.data.x.std(), dataset.data.y.std())
36-
for radius in np.linspace(max_radius // 5, max_radius, num=5):
37-
for angle in np.linspace(0, 360, num=50, endpoint=False):
38-
points.append(
39-
(
40-
center[0]
41-
+ np.cos(angle) * radius
42-
+ rng.standard_normal() * max_radius,
43-
center[1]
44-
+ np.sin(angle) * radius
45-
+ rng.standard_normal() * max_radius,
46-
)
36+
points.extend(
37+
[
38+
(
39+
center[0]
40+
+ np.cos(angle) * radius
41+
+ rng.standard_normal() * max_radius,
42+
center[1]
43+
+ np.sin(angle) * radius
44+
+ rng.standard_normal() * max_radius,
4745
)
46+
for radius in np.linspace(max_radius // 5, max_radius, num=5)
47+
for angle in np.linspace(0, 360, num=50, endpoint=False)
48+
]
49+
)
4850
super().__init__(*points)
4951

5052
self._alpha = 0.4

tests/bounds/test_bounding_box.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class TestBoundingBox:
1414

1515
@pytest.mark.input_validation
1616
@pytest.mark.parametrize(
17-
['x_bounds', 'y_bounds'],
17+
('x_bounds', 'y_bounds'),
1818
[
19-
[None, None],
20-
[[0, 1], None],
21-
[None, [0, 1]],
22-
[Interval([0, 1]), None],
23-
[None, Interval([0, 1])],
19+
(None, None),
20+
([0, 1], None),
21+
(None, [0, 1]),
22+
(Interval([0, 1]), None),
23+
(None, Interval([0, 1])),
2424
],
2525
ids=[
2626
'neither',
@@ -43,16 +43,16 @@ def test_input_validation_inclusive(self, inclusive):
4343
_ = BoundingBox([0, 1], [0, 1], inclusive)
4444

4545
@pytest.mark.parametrize(
46-
['x_bounds', 'y_bounds', 'inclusive', 'expected'],
46+
('x_bounds', 'y_bounds', 'inclusive', 'expected'),
4747
[
48-
[[0, 1], [0, 1], True, (True, True)],
49-
[[0, 1], [0, 1], False, (False, False)],
50-
[Interval([0, 1], True), Interval([0, 1], True), True, (True, True)],
51-
[Interval([0, 1], True), Interval([0, 1], True), False, (True, True)],
52-
[Interval([0, 1], True), [0, 1], True, (True, True)],
53-
[[0, 1], Interval([0, 1], True), True, (True, True)],
54-
[Interval([0, 1], True), [0, 1], False, (True, False)],
55-
[[0, 1], Interval([0, 1], True), False, (False, True)],
48+
([0, 1], [0, 1], True, (True, True)),
49+
([0, 1], [0, 1], False, (False, False)),
50+
(Interval([0, 1], True), Interval([0, 1], True), True, (True, True)),
51+
(Interval([0, 1], True), Interval([0, 1], True), False, (True, True)),
52+
(Interval([0, 1], True), [0, 1], True, (True, True)),
53+
([0, 1], Interval([0, 1], True), True, (True, True)),
54+
(Interval([0, 1], True), [0, 1], False, (True, False)),
55+
([0, 1], Interval([0, 1], True), False, (False, True)),
5656
],
5757
)
5858
def test_init(self, x_bounds, y_bounds, inclusive, expected):
@@ -74,12 +74,12 @@ def test_init(self, x_bounds, y_bounds, inclusive, expected):
7474
assert bbox.y_bounds != y_bounds
7575

7676
@pytest.mark.parametrize(
77-
['value', 'inclusive', 'expected'],
77+
('value', 'inclusive', 'expected'),
7878
[
79-
[[1, 1], True, True],
80-
[[1, 1], False, True],
81-
[[0, 0], True, True],
82-
[[0, 0], False, False],
79+
([1, 1], True, True),
80+
([1, 1], False, True),
81+
([0, 0], True, True),
82+
([0, 0], False, False),
8383
],
8484
ids=[
8585
'inside box - inclusive',
@@ -137,13 +137,13 @@ def test_repr(self):
137137

138138
@pytest.mark.input_validation
139139
@pytest.mark.parametrize(
140-
['x', 'y'],
140+
('x', 'y'),
141141
[
142-
[True, True],
143-
[None, True],
144-
['s', None],
145-
[None, 's'],
146-
['s', 's'],
142+
(True, True),
143+
(None, True),
144+
('s', None),
145+
(None, 's'),
146+
('s', 's'),
147147
],
148148
)
149149
def test_adjust_bounds_input_validation(self, x, y):
@@ -152,7 +152,7 @@ def test_adjust_bounds_input_validation(self, x, y):
152152
with pytest.raises(TypeError, match='value must be a numeric value'):
153153
bbox.adjust_bounds(x, y)
154154

155-
@pytest.mark.parametrize(['x', 'y'], [[10, 10], [0, 10], [10, 0]])
155+
@pytest.mark.parametrize(('x', 'y'), [(10, 10), (0, 10), (10, 0)])
156156
def test_adjust_bounds(self, x, y):
157157
"""Test that the adjust_bounds() method is working."""
158158
start = [10, 90]
@@ -165,7 +165,7 @@ def test_adjust_bounds(self, x, y):
165165
assert new_range_y == initial_range_y + y
166166

167167
@pytest.mark.parametrize(
168-
['x', 'y'],
168+
('x', 'y'),
169169
[
170170
([10, 90], [500, 600]),
171171
([500, 600], [10, 90]),
@@ -188,7 +188,7 @@ def test_clone(self):
188188
assert bbox1 != bbox2
189189

190190
@pytest.mark.parametrize(
191-
['x', 'y', 'expected'],
191+
('x', 'y', 'expected'),
192192
[
193193
([10, 90], [500, 600], 0.8),
194194
([500, 600], [10, 90], 1.25),

tests/bounds/test_interval.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_invalid(self, limits):
2828
_ = Interval(limits)
2929

3030
@pytest.mark.parametrize(
31-
['limits', 'inclusive', 'value', 'expected'],
31+
('limits', 'inclusive', 'value', 'expected'),
3232
[
3333
([0, 10], True, 0, True),
3434
([0, 10], True, 10, True),
@@ -63,7 +63,7 @@ def test_contains_invalid(self, value):
6363
_ = value in Interval([0, 10])
6464

6565
@pytest.mark.parametrize(
66-
['limits', 'inclusive', 'expected'],
66+
('limits', 'inclusive', 'expected'),
6767
[
6868
([0, 1], True, True),
6969
([0, 1], False, False),
@@ -82,7 +82,7 @@ def test_eq_type(self, other):
8282
_ = Interval([0, 1], True) == other
8383

8484
def test_getitem(self):
85-
"""Test thatthe __getitem__() method is working."""
85+
"""Test that the __getitem__() method is working."""
8686
limits = [0, 1]
8787
bounds = Interval(limits)
8888
assert bounds[0] == limits[0]
@@ -95,7 +95,7 @@ def test_iter(self):
9595
assert bound == limit
9696

9797
@pytest.mark.parametrize(
98-
['inclusive', 'expected'],
98+
('inclusive', 'expected'),
9999
[
100100
(True, '<Interval inclusive [0, 1]>'),
101101
(False, '<Interval exclusive (0, 1)>'),
@@ -108,7 +108,7 @@ def test_repr(self, inclusive, expected):
108108

109109
@pytest.mark.input_validation
110110
@pytest.mark.parametrize(
111-
['value', 'expected_msg', 'exc_class'],
111+
('value', 'expected_msg', 'exc_class'),
112112
[
113113
(0, 'value must be non-zero', ValueError),
114114
(None, 'value must be a numeric value', TypeError),
@@ -136,7 +136,7 @@ def test_adjust_bounds(self, value):
136136
assert bounds[1] == start[1] + value / 2
137137

138138
@pytest.mark.parametrize(
139-
['limits', 'inclusive'],
139+
('limits', 'inclusive'),
140140
[
141141
([10, 90], True),
142142
([10, 90], False),
@@ -160,7 +160,7 @@ def test_clone(self, limits, inclusive):
160160

161161
@pytest.mark.parametrize('inclusive', [True, False])
162162
@pytest.mark.parametrize(
163-
['limits', 'expected'],
163+
('limits', 'expected'),
164164
[
165165
([-10, -5], 5),
166166
([-1, 1], 2),

tests/bounds/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@pytest.mark.bounds
99
@pytest.mark.input_validation
1010
@pytest.mark.parametrize(
11-
['data', 'msg'],
11+
('data', 'msg'),
1212
[
1313
(True, 'must be an iterable of 2 numeric values'),
1414
({1, 2}, 'must be an iterable of 2 numeric values'),

tests/data/test_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_validate_data_fix_column_casing(self, starter_shapes_dir):
6666

6767
@pytest.mark.bounds
6868
@pytest.mark.parametrize(
69-
['scale', 'data_bounds', 'morph_bounds', 'plot_bounds'],
69+
('scale', 'data_bounds', 'morph_bounds', 'plot_bounds'),
7070
[
7171
(
7272
10,

tests/data/test_loader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_static_class(self):
1717

1818
@pytest.mark.dataset
1919
@pytest.mark.parametrize(
20-
['name', 'file'], [['dino', 'dino.csv'], ['sheep', 'sheep.csv']]
20+
('name', 'file'), [('dino', 'dino.csv'), ('sheep', 'sheep.csv')]
2121
)
2222
def test_load_dataset(self, name, file, starter_shapes_dir):
2323
"""Confirm that loading the dataset by name and file works."""
@@ -37,8 +37,8 @@ def test_load_dataset_unknown_data(self, dataset):
3737
_ = DataLoader.load_dataset(dataset)
3838

3939
@pytest.mark.parametrize(
40-
['provided_name', 'expected_name'],
41-
[['python', 'Python'], ['Python', 'Python'], ['sds', 'SDS'], ['SDS', 'SDS']],
40+
('provided_name', 'expected_name'),
41+
[('python', 'Python'), ('Python', 'Python'), ('sds', 'SDS'), ('SDS', 'SDS')],
4242
)
4343
def test_load_dataset_proper_nouns(self, provided_name, expected_name):
4444
"""

0 commit comments

Comments
 (0)