Skip to content

Commit cddaa1e

Browse files
authored
Use generic alias types instead of deprecated typing aliases (#15)
* refactor: replace deprecated `typing` type aliases to generic alias types * fix: make some string translatable again - change return type in `create_status_page` to `Adw.StatusPage`
1 parent a3e651d commit cddaa1e

File tree

9 files changed

+40
-40
lines changed

9 files changed

+40
-40
lines changed

gradia/graphics/gradient.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717

1818
import ctypes
1919
from ctypes import c_int, c_double, c_uint8, POINTER, CDLL
20-
from typing import Dict, Tuple, List, Optional, Callable, Union
20+
from collections.abc import Callable
21+
from typing import Optional
2122
from PIL import Image
2223
from gi.repository import Gtk, Gdk, Adw
2324

2425

2526
HexColor = str
26-
RGBTuple = Tuple[int, int, int]
27-
CacheKey = Tuple[str, str, int, int, int]
28-
GradientPreset = Tuple[str, str, int]
29-
CacheInfo = Dict[str, Union[int, List[CacheKey], bool]]
27+
RGBTuple = tuple[int, int, int]
28+
CacheKey = tuple[str, str, int, int, int]
29+
GradientPreset = tuple[str, str, int]
30+
CacheInfo = dict[str, int | list[CacheKey] | bool]
3031

3132

3233
class GradientBackground:
3334
_MAX_CACHE_SIZE: int = 100
34-
_gradient_cache: Dict[CacheKey, Image.Image] = {}
35-
_c_lib: Optional[Union[CDLL, bool]] = None
35+
_gradient_cache: dict[CacheKey, Image.Image] = {}
36+
_c_lib: Optional[CDLL | bool] = None
3637

3738
@classmethod
3839
def _load_c_lib(cls) -> None:
@@ -119,7 +120,7 @@ def get_cache_info(cls) -> CacheInfo:
119120

120121

121122
class GradientSelector:
122-
PREDEFINED_GRADIENTS: List[GradientPreset] = [
123+
PREDEFINED_GRADIENTS: list[GradientPreset] = [
123124
("#36d1dc", "#5b86e5", 90),
124125
("#ff5f6d", "#ffc371", 45),
125126
("#453383", "#5494e8", 0),

gradia/graphics/image_processor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import os
1919
import io
20-
from typing import Optional, Tuple, Union
20+
from typing import Optional
2121
from PIL import Image, ImageDraw, ImageChops, ImageFilter, ImageOps
2222
from gi.repository import GdkPixbuf
2323

@@ -32,7 +32,7 @@ def __init__(
3232
image_path: Optional[str] = None,
3333
background: Optional[object] = None,
3434
padding: int = 5,
35-
aspect_ratio: Optional[Union[str, float]] = None,
35+
aspect_ratio: Optional[str | float] = None,
3636
corner_radius: int = 2,
3737
shadow_strength: float = 0.0
3838
) -> None:
@@ -98,7 +98,7 @@ def _load_and_downscale_image(self, image_path: str) -> Image.Image:
9898

9999
return source_img
100100

101-
def _compress_image_with_size(self, image: Image.Image, quality: int) -> Tuple[Image.Image, int]:
101+
def _compress_image_with_size(self, image: Image.Image, quality: int) -> tuple[Image.Image, int]:
102102
buffer = io.BytesIO()
103103
image.save(buffer, format='PNG', optimize=True, quality=quality)
104104
size = buffer.tell()
@@ -131,7 +131,7 @@ def _crop_image(self, image: Image.Image) -> Image.Image:
131131
offset_y = (height - crop_h) // 2
132132
return image.crop((offset_x, offset_y, offset_x + crop_w, offset_y + crop_h))
133133

134-
def _calculate_final_dimensions(self, width: int, height: int) -> Tuple[int, int]:
134+
def _calculate_final_dimensions(self, width: int, height: int) -> tuple[int, int]:
135135
if self.padding >= 0:
136136
smaller_dimension = min(width, height)
137137
padding_percentage = self._get_percentage(self.padding)
@@ -144,7 +144,7 @@ def _calculate_final_dimensions(self, width: int, height: int) -> Tuple[int, int
144144

145145
return width, height
146146

147-
def _adjust_for_aspect_ratio(self, width: int, height: int) -> Tuple[int, int]:
147+
def _adjust_for_aspect_ratio(self, width: int, height: int) -> tuple[int, int]:
148148
try:
149149
ratio = self._parse_aspect_ratio()
150150
current = width / height
@@ -189,7 +189,7 @@ def _create_background(self, width: int, height: int) -> Image.Image:
189189
return self.background.prepare_image(width, height)
190190
return Image.new("RGBA", (width, height), (0, 0, 0, 0))
191191

192-
def _create_shadow(self, image: Image.Image, offset: Tuple[int, int] = (10, 10), shadow_strength: float = 1.0) -> Tuple[Image.Image, Tuple[int, int]]:
192+
def _create_shadow(self, image: Image.Image, offset: tuple[int, int] = (10, 10), shadow_strength: float = 1.0) -> tuple[Image.Image, tuple[int, int]]:
193193
shadow_strength = max(0.0, min(shadow_strength, 1.0))
194194
blur_radius = int(10 * shadow_strength)
195195
shadow_alpha = int(150 * shadow_strength)
@@ -211,7 +211,7 @@ def _create_shadow(self, image: Image.Image, offset: Tuple[int, int] = (10, 10),
211211
shadow_canvas = shadow_canvas.filter(ImageFilter.GaussianBlur(blur_radius))
212212
return shadow_canvas, (shadow_x, shadow_y)
213213

214-
def _get_paste_position(self, img_w: int, img_h: int, bg_w: int, bg_h: int) -> Tuple[int, int]:
214+
def _get_paste_position(self, img_w: int, img_h: int, bg_w: int, bg_h: int) -> tuple[int, int]:
215215
if self.padding >= 0:
216216
x = (bg_w - img_w) // 2
217217
y = (bg_h - img_h) // 2

gradia/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import tempfile
2020
import shutil
2121

22-
from typing import Sequence
22+
from collections.abc import Sequence
2323

2424
from gi.repository import Adw, Gio
2525

gradia/ui/drawing_actions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# SPDX-License-Identifier: GPL-3.0-or-later
1717

1818
from gi.repository import Gtk, Gdk, Gio, cairo, Pango, PangoCairo
19-
from typing import Tuple, List, Optional, Union
2019
from enum import Enum
2120
import math
2221
import re

gradia/ui/drawing_overlay.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# SPDX-License-Identifier: GPL-3.0-or-later
1717

1818
from gi.repository import Gtk, Gdk, Gio, cairo, Pango, PangoCairo
19-
from typing import Tuple, List, Optional, Union
2019
from enum import Enum
2120
from gradia.ui.drawing_actions import *
2221
import math
@@ -218,7 +217,7 @@ def _show_text_entry(self, x, y):
218217
hbox.add_css_class("linked")
219218

220219
entry = Gtk.Entry()
221-
entry.set_placeholder_text("Enter text...")
220+
entry.set_placeholder_text(_("Enter text…"))
222221
entry.set_width_chars(12)
223222
entry.connect("activate", self._on_text_entry_activate)
224223
entry.connect("changed", self._on_text_entry_changed)

gradia/ui/image_exporters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
# SPDX-License-Identifier: GPL-3.0-or-later
1717

1818
import os
19-
from typing import Tuple
19+
2020
from gi.repository import Gtk, Gio, GdkPixbuf
2121
from gradia.clipboard import copy_file_to_clipboard, save_pixbuff_to_path
2222

23-
ExportFormat = Tuple[str, str, str]
23+
ExportFormat = tuple[str, str, str]
2424

2525
class BaseImageExporter:
2626
"""Base class for image export handlers"""

gradia/ui/image_loaders.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818

1919
import os
20-
from typing import Optional, Tuple
20+
from typing import Optional
21+
2122
from gi.repository import Gtk, Gio, Gdk, GLib, Xdp
2223
from gradia.clipboard import save_texture_to_file
2324

24-
ImportFormat = Tuple[str, str]
25+
ImportFormat = tuple[str, str]
2526

2627
class BaseImageLoader:
2728
"""Base class for image loading handlers"""

gradia/ui/ui_parts.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#
1616
# SPDX-License-Identifier: GPL-3.0-or-later
1717

18-
from typing import Callable, Dict, Optional, Tuple, Union
18+
from collections.abc import Callable
19+
from typing import Optional
1920
from gi.repository import Gtk, Gio, Adw, Gdk, GLib
2021

2122
from gradia.ui.drawing_actions import DrawingMode
@@ -77,7 +78,7 @@ def create_header_bar() -> Adw.HeaderBar:
7778
return header_bar
7879

7980

80-
def create_image_stack() -> Tuple[Gtk.Stack, Gtk.Picture, Adw.Spinner, 'DrawingOverlay']:
81+
def create_image_stack() -> tuple[Gtk.Stack, Gtk.Picture, Adw.Spinner, 'DrawingOverlay']:
8182
stack = Gtk.Stack.new()
8283
stack.set_vexpand(True)
8384
stack.set_hexpand(True)
@@ -110,7 +111,7 @@ def create_image_overlay(picture: Gtk.Picture, drawing_overlay: 'DrawingOverlay'
110111

111112
return overlay
112113

113-
def create_controls_overlay() -> Gtk.Widget:
114+
def create_controls_overlay() -> Gtk.Box:
114115
undo_btn = Gtk.Button.new_from_icon_name("edit-undo-symbolic")
115116
undo_btn.set_tooltip_text(_("Undo the last action"))
116117

@@ -156,7 +157,7 @@ def create_drawing_overlay(picture: Gtk.Picture) -> 'DrawingOverlay':
156157
drawing_overlay.set_picture_reference(picture)
157158
return drawing_overlay
158159

159-
def create_spinner_widget() -> Gtk.Widget:
160+
def create_spinner_widget() -> tuple[Gtk.Box, Adw.Spinner]:
160161
spinner = Adw.Spinner.new()
161162
spinner.set_size_request(48, 48)
162163

@@ -173,8 +174,8 @@ def create_spinner_widget() -> Gtk.Widget:
173174
spinner_box.append(spinner)
174175
return spinner_box, spinner
175176

176-
def create_status_page() -> Gtk.Widget:
177-
screenshot_btn = Gtk.Button.new_with_label("_Take a screenshot…")
177+
def create_status_page() -> Adw.StatusPage:
178+
screenshot_btn = Gtk.Button.new_with_label(_("_Take a screenshot…"))
178179
screenshot_btn.set_use_underline(True)
179180
screenshot_btn.set_halign(Gtk.Align.CENTER)
180181

@@ -185,7 +186,7 @@ def create_status_page() -> Gtk.Widget:
185186

186187
screenshot_btn.set_action_name("app.screenshot")
187188

188-
open_status_btn = Gtk.Button.new_with_label("_Open Image…")
189+
open_status_btn = Gtk.Button.new_with_label(_("_Open Image…"))
189190
open_status_btn.set_use_underline(True)
190191
open_status_btn.set_halign(Gtk.Align.CENTER)
191192

@@ -202,8 +203,8 @@ def create_status_page() -> Gtk.Widget:
202203

203204
status_page = Adw.StatusPage.new()
204205
status_page.set_icon_name("image-x-generic-symbolic")
205-
status_page.set_title("No Image Loaded")
206-
status_page.set_description("Drag and drop one here")
206+
status_page.set_title(_("No Image Loaded"))
207+
status_page.set_description(_("Drag and drop one here"))
207208
status_page.set_child(button_box)
208209

209210
return status_page
@@ -223,14 +224,12 @@ def on_file_dropped(_target: Gtk.DropTarget, value: Gio.File, _x: int, _y: int)
223224
drop_target.connect("drop", on_file_dropped)
224225
stack.add_controller(drop_target)
225226

226-
227-
228227
def create_image_options_group(
229228
on_padding_changed: Callable[[Adw.SpinRow], None],
230229
on_aspect_ratio_changed: Callable[[Gtk.Entry], None],
231230
on_corner_radius_changed: Callable[[Adw.SpinRow], None],
232231
on_shadow_strength_changed: Callable[[Gtk.Scale], None]
233-
) -> Tuple[Adw.PreferencesGroup, Adw.SpinRow, Gtk.Entry]:
232+
) -> tuple[Adw.PreferencesGroup, Adw.SpinRow, Gtk.Entry]:
234233
padding_group = Adw.PreferencesGroup(title=_("Image Options"))
235234

236235
padding_adjustment = Gtk.Adjustment(value=5, lower=-25, upper=75, step_increment=5, page_increment=5)
@@ -267,12 +266,12 @@ def create_image_options_group(
267266

268267
return padding_group, padding_row, aspect_ratio_entry
269268

270-
def create_file_info_group() -> Tuple[Adw.PreferencesGroup, Adw.ActionRow, Adw.ActionRow, Adw.ActionRow]:
271-
file_info_group = Adw.PreferencesGroup(title="Current File")
269+
def create_file_info_group() -> tuple[Adw.PreferencesGroup, Adw.ActionRow, Adw.ActionRow, Adw.ActionRow]:
270+
file_info_group = Adw.PreferencesGroup(title=_("Current File"))
272271

273272
filename_row = Adw.ActionRow(title=_("Name"), subtitle=_("No file loaded"))
274273
location_row = Adw.ActionRow(title=_("Location"), subtitle=_("No file loaded"))
275-
processed_size_row = Adw.ActionRow(title=_("Modified image size"), subtitle="N/A")
274+
processed_size_row = Adw.ActionRow(title=_("Modified image size"), subtitle=_("N/A"))
276275

277276
file_info_group.add(filename_row)
278277
file_info_group.add(location_row)
@@ -407,7 +406,7 @@ def create_sidebar_ui(
407406
on_corner_radius_changed: Callable[[Adw.SpinRow], None],
408407
on_aspect_ratio_changed: Callable[[Gtk.Entry], None],
409408
on_shadow_strength_changed: Callable[[Gtk.Scale], None],
410-
) -> Dict[str, Union[Gtk.Widget, Adw.ActionRow, Adw.SpinRow, Gtk.Entry]]:
409+
) -> dict[str, Gtk.Widget | Adw.ActionRow | Adw.SpinRow | Gtk.Entry]:
411410
sidebar_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
412411
settings_scroll = Gtk.ScrolledWindow(vexpand=True)
413412
controls_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20,

gradia/ui/window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
import os
1919
import threading
20-
from typing import Optional, Callable, Any, Union
20+
from collections.abc import Callable
21+
from typing import Optional, Any
2122

2223
from gi.repository import Gtk, Gio, Adw, Gdk, GLib
2324
from gradia.graphics.image_processor import ImageProcessor

0 commit comments

Comments
 (0)