Skip to content

Commit 7ba6e90

Browse files
committed
Fix Ruff lints
Signed-off-by: Benjamin Gilbert <bgilbert@cs.cmu.edu>
1 parent cac2c95 commit 7ba6e90

9 files changed

Lines changed: 45 additions & 34 deletions

File tree

examples/deepzoom/deepzoom_multiserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def tile(path: str, level: int, col: int, row: int, format: str) -> Response:
192192
icc_profile=tile.info.get('icc_profile'),
193193
)
194194
resp = make_response(buf.getvalue())
195-
resp.mimetype = 'image/%s' % format
195+
resp.mimetype = f'image/{format}'
196196
return resp
197197

198198
return app

examples/deepzoom/deepzoom_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def tile(slug: str, level: int, col: int, row: int, format: str) -> Response:
193193
icc_profile=tile.info.get('icc_profile'),
194194
)
195195
resp = make_response(buf.getvalue())
196-
resp.mimetype = 'image/%s' % format
196+
resp.mimetype = f'image/{format}'
197197
return resp
198198

199199
return app

examples/deepzoom/deepzoom_tile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ def _tile_done(self) -> None:
231231
count, total = self._processed, self._dz.tile_count
232232
if count % 100 == 0 or count == total:
233233
print(
234-
'Tiling %s: wrote %d/%d tiles'
235-
% (self._associated or 'slide', count, total),
234+
f'Tiling {self._associated or "slide"}: wrote {count}/{total} tiles',
236235
end='\r',
237236
file=sys.stderr,
238237
)
@@ -320,7 +319,7 @@ def _url_for(self, associated: str | None) -> str:
320319
base = VIEWER_SLIDE_NAME
321320
else:
322321
base = self._slugify(associated)
323-
return '%s.dzi' % base
322+
return f'{base}.dzi'
324323

325324
def _write_html(self) -> None:
326325
import jinja2

openslide/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ def get_thumbnail(self, size: tuple[int, int]) -> Image.Image:
169169
"""Return a PIL.Image containing an RGB thumbnail of the image.
170170
171171
size: the maximum size of the thumbnail."""
172-
downsample = max(dim / thumb for dim, thumb in zip(self.dimensions, size))
172+
downsample = max(
173+
dim / thumb for dim, thumb in zip(self.dimensions, size, strict=True)
174+
)
173175
level = self.get_best_level_for_downsample(downsample)
174176
tile = self.read_region((0, 0), level, self.level_dimensions[level])
175177
# Apply on solid background
@@ -460,20 +462,25 @@ def read_region(
460462
# the image. Create a transparent tile of the correct size and
461463
# paste the valid part of the region into the correct location.
462464
image_topleft = [
463-
max(0, min(l, limit - 1)) for l, limit in zip(location, self._image.size)
465+
max(0, min(l, limit - 1))
466+
for l, limit in zip(location, self._image.size, strict=True)
464467
]
465468
image_bottomright = [
466469
max(0, min(l + s - 1, limit - 1))
467-
for l, s, limit in zip(location, size, self._image.size)
470+
for l, s, limit in zip(location, size, self._image.size, strict=True)
468471
]
469472
tile = Image.new('RGBA', size, (0,) * 4)
470473
if not [
471-
'fail' for tl, br in zip(image_topleft, image_bottomright) if br - tl < 0
474+
'fail'
475+
for tl, br in zip(image_topleft, image_bottomright, strict=True)
476+
if br - tl < 0
472477
]: # "< 0" not a typo
473478
# Crop size is greater than zero in both dimensions.
474479
# PIL thinks the bottom right is the first *excluded* pixel
475480
crop_box = tuple(image_topleft + [d + 1 for d in image_bottomright])
476-
tile_offset = tuple(il - l for il, l in zip(image_topleft, location))
481+
tile_offset = tuple(
482+
il - l for il, l in zip(image_topleft, location, strict=True)
483+
)
477484
assert len(crop_box) == 4 and len(tile_offset) == 2
478485
crop = self._image.crop(crop_box)
479486
tile.paste(crop, tile_offset)

openslide/deepzoom.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ def __init__(
8888
# Slide level dimensions scale factor in each axis
8989
size_scale = tuple(
9090
int(osr.properties.get(prop, l0_lim)) / l0_lim
91-
for prop, l0_lim in zip(self.BOUNDS_SIZE_PROPS, osr.dimensions)
91+
for prop, l0_lim in zip(
92+
self.BOUNDS_SIZE_PROPS, osr.dimensions, strict=True
93+
)
9294
)
9395
# Dimensions of active area
9496
self._l_dimensions = tuple(
9597
tuple(
96-
int(math.ceil(l_lim * scale))
97-
for l_lim, scale in zip(l_size, size_scale)
98+
math.ceil(l_lim * scale)
99+
for l_lim, scale in zip(l_size, size_scale, strict=True)
98100
)
99101
for l_size in osr.level_dimensions
100102
)
@@ -106,14 +108,14 @@ def __init__(
106108
z_size = self._l0_dimensions
107109
z_dimensions = [z_size]
108110
while z_size[0] > 1 or z_size[1] > 1:
109-
z_size = tuple(max(1, int(math.ceil(z / 2))) for z in z_size)
111+
z_size = tuple(max(1, math.ceil(z / 2)) for z in z_size)
110112
z_dimensions.append(z_size)
111113
# Narrow the type, for self.level_dimensions
112114
self._z_dimensions = self._pairs_from_n_tuples(tuple(reversed(z_dimensions)))
113115

114116
# Tile
115117
def tiles(z_lim: int) -> int:
116-
return int(math.ceil(z_lim / self._z_t_downsample))
118+
return math.ceil(z_lim / self._z_t_downsample)
117119

118120
self._t_dimensions = tuple(
119121
(tiles(z_w), tiles(z_h)) for z_w, z_h in self._z_dimensions
@@ -203,7 +205,7 @@ def _get_tile_info(
203205
# Check parameters
204206
if dz_level < 0 or dz_level >= self._dz_levels:
205207
raise ValueError('Invalid level')
206-
for t, t_lim in zip(t_location, self._t_dimensions[dz_level]):
208+
for t, t_lim in zip(t_location, self._t_dimensions[dz_level], strict=True):
207209
if t < 0 or t >= t_lim:
208210
raise ValueError('Invalid address')
209211

@@ -214,31 +216,37 @@ def _get_tile_info(
214216
z_overlap_tl = tuple(self._z_overlap * int(t != 0) for t in t_location)
215217
z_overlap_br = tuple(
216218
self._z_overlap * int(t != t_lim - 1)
217-
for t, t_lim in zip(t_location, self.level_tiles[dz_level])
219+
for t, t_lim in zip(t_location, self.level_tiles[dz_level], strict=True)
218220
)
219221

220222
# Get final size of the tile
221223
z_size = tuple(
222224
min(self._z_t_downsample, z_lim - self._z_t_downsample * t) + z_tl + z_br
223225
for t, z_lim, z_tl, z_br in zip(
224-
t_location, self._z_dimensions[dz_level], z_overlap_tl, z_overlap_br
226+
t_location,
227+
self._z_dimensions[dz_level],
228+
z_overlap_tl,
229+
z_overlap_br,
230+
strict=True,
225231
)
226232
)
227233

228234
# Obtain the region coordinates
229235
z_location = [self._z_from_t(t) for t in t_location]
230236
l_location = [
231237
self._l_from_z(dz_level, z - z_tl)
232-
for z, z_tl in zip(z_location, z_overlap_tl)
238+
for z, z_tl in zip(z_location, z_overlap_tl, strict=True)
233239
]
234240
# Round location down and size up, and add offset of active area
235241
l0_location = tuple(
236242
int(self._l0_from_l(slide_level, l) + l0_off)
237-
for l, l0_off in zip(l_location, self._l0_offset)
243+
for l, l0_off in zip(l_location, self._l0_offset, strict=True)
238244
)
239245
l_size = tuple(
240246
int(min(math.ceil(self._l_from_z(dz_level, dz)), l_lim - math.ceil(l)))
241-
for l, dz, l_lim in zip(l_location, z_size, self._l_dimensions[slide_level])
247+
for l, dz, l_lim in zip(
248+
l_location, z_size, self._l_dimensions[slide_level], strict=True
249+
)
242250
)
243251

244252
# Return read_region() parameters plus tile size for final scaling

openslide/lowlevel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def decorator(fn: Callable[_P, _T]) -> _Func[_P, _T]:
379379
'openslide_detect_vendor', c_char_p, [_filename_p], _check_string
380380
)
381381
except AttributeError:
382-
raise OpenSlideVersionError('3.4.0')
382+
raise OpenSlideVersionError('3.4.0') from None
383383

384384
open: _Func[[Filename], _OpenSlide] = _func(
385385
'openslide_open', c_void_p, [_filename_p], _check_open
@@ -434,7 +434,7 @@ def read_region(
434434
# OpenSlide would catch this, but not before we tried to allocate
435435
# a negative-size buffer
436436
raise OpenSlideError(
437-
'negative width (%d) or negative height (%d) not allowed' % (w, h)
437+
f'negative width ({w}) or negative height ({h}) not allowed'
438438
)
439439
if w == 0 or h == 0:
440440
# Image.frombuffer() would raise an exception

tests/test_deepzoom.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def tearDown(self) -> None:
4444
def test_repr(self) -> None:
4545
self.assertEqual(
4646
repr(self.dz),
47-
'DeepZoomGenerator(%r, tile_size=254, overlap=1, limit_bounds=False)'
48-
% self.osr,
47+
f'DeepZoomGenerator({self.osr!r}, tile_size=254, overlap=1, limit_bounds=False)',
4948
)
5049

5150
def test_metadata(self) -> None:

tests/test_imageslide.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_open_image(self) -> None:
4242
with Image.open(file_path('boxes.png')) as img:
4343
with ImageSlide(img) as osr:
4444
self.assertEqual(osr.dimensions, (300, 250))
45-
self.assertEqual(repr(osr), 'ImageSlide(%r)' % img)
45+
self.assertEqual(repr(osr), f'ImageSlide({img!r})')
4646

4747
@unittest.skipUnless(
4848
sys.getfilesystemencoding() == 'utf-8',
@@ -96,7 +96,7 @@ class TestImage(_Abstract.SlideTest):
9696
FILENAME = 'boxes.png'
9797

9898
def test_repr(self) -> None:
99-
self.assertEqual(repr(self.osr), 'ImageSlide(%r)' % file_path('boxes.png'))
99+
self.assertEqual(repr(self.osr), f'ImageSlide({file_path("boxes.png")!r})')
100100

101101
def test_metadata(self) -> None:
102102
self.assertEqual(self.osr.level_count, 1)

tests/test_openslide.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class TestSlide(_Abstract.SlideTest):
124124
FILENAME = 'boxes.tiff'
125125

126126
def test_repr(self) -> None:
127-
self.assertEqual(repr(self.osr), 'OpenSlide(%r)' % file_path('boxes.tiff'))
127+
self.assertEqual(repr(self.osr), f'OpenSlide({file_path("boxes.tiff")!r})')
128128

129129
def test_basic_metadata(self) -> None:
130130
self.assertEqual(self.osr.level_count, 4)
@@ -146,11 +146,9 @@ def test_properties(self) -> None:
146146
self.assertEqual(self.osr.properties['openslide.vendor'], 'generic-tiff')
147147
self.assertRaises(KeyError, lambda: self.osr.properties['__does_not_exist'])
148148
# test __len__ and __iter__
149+
self.assertEqual(len(list(self.osr.properties)), len(self.osr.properties))
149150
self.assertEqual(
150-
len([v for v in self.osr.properties]), len(self.osr.properties)
151-
)
152-
self.assertEqual(
153-
repr(self.osr.properties), '<_PropertyMap %r>' % dict(self.osr.properties)
151+
repr(self.osr.properties), f'<_PropertyMap {dict(self.osr.properties)!r}>'
154152
)
155153

156154
@unittest.skipUnless(
@@ -215,7 +213,7 @@ def test_associated_images(self) -> None:
215213
self.assertRaises(KeyError, lambda: self.osr.associated_images['__missing'])
216214
# test __len__ and __iter__
217215
self.assertEqual(
218-
len([v for v in self.osr.associated_images]),
216+
len(list(self.osr.associated_images)),
219217
len(self.osr.associated_images),
220218
)
221219

@@ -224,7 +222,7 @@ def mangle_repr(o: Any) -> str:
224222

225223
self.assertEqual(
226224
mangle_repr(self.osr.associated_images),
227-
'<_AssociatedImageMap %s>' % mangle_repr(dict(self.osr.associated_images)),
225+
f'<_AssociatedImageMap {mangle_repr(dict(self.osr.associated_images))}>',
228226
)
229227

230228
def test_color_profile(self) -> None:

0 commit comments

Comments
 (0)