Skip to content

Commit dcfb7ad

Browse files
committed
Add static typing for Preferences
1 parent 92da124 commit dcfb7ad

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/diffuse/preferences.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import sys
2525

2626
from gettext import gettext as _
27-
from typing import List
27+
from typing import Any, Dict, Final, List, Optional, Tuple
2828

2929
from diffuse import constants
3030
from diffuse import utils
@@ -36,16 +36,16 @@
3636

3737
# class to store preferences and construct a dialogue for manipulating them
3838
class Preferences:
39-
def __init__(self, path):
40-
self.bool_prefs = {}
41-
self.int_prefs = {}
42-
self.string_prefs = {}
43-
self.int_prefs_min = {}
44-
self.int_prefs_max = {}
45-
self.string_prefs_enums = {}
39+
def __init__(self, path: str) -> None:
40+
self.path = path
41+
self.bool_prefs: Dict[str, bool] = {}
42+
self.string_prefs: Dict[str, str] = {}
43+
self.int_prefs: Dict[str, int] = {}
44+
self.int_prefs_min: Dict[str, int] = {}
45+
self.int_prefs_max: Dict[str, int] = {}
4646

4747
# find available encodings
48-
self.encodings = sorted(set(encodings.aliases.aliases.values()))
48+
self.encodings: List[Optional[str]] = sorted(set(encodings.aliases.aliases.values()))
4949

5050
if utils.isWindows():
5151
svk_bin = 'svk.bat'
@@ -54,7 +54,7 @@ def __init__(self, path):
5454

5555
auto_detect_codecs = ['utf_8', 'utf_16', 'latin_1']
5656
e = utils.norm_encoding(sys.getfilesystemencoding())
57-
if e not in auto_detect_codecs:
57+
if e is not None and e not in auto_detect_codecs:
5858
# insert after UTF-8 as the default encoding may prevent UTF-8 from
5959
# being tried
6060
auto_detect_codecs.insert(2, e)
@@ -124,7 +124,7 @@ def __init__(self, path):
124124
]
125125

126126
# conditions used to determine if a preference should be greyed out
127-
self.disable_when = {
127+
self.disable_when: Final[Dict[str, Tuple[str, bool]]] = {
128128
'display_right_margin': ('display_show_right_margin', False),
129129
'display_ignore_whitespace_changes': ('display_ignore_whitespace', True),
130130
'display_ignore_blanklines': ('display_ignore_whitespace', True),
@@ -165,9 +165,9 @@ def __init__(self, path):
165165
_('Version control system search order')
166166
]
167167
]
168-
vcs_folders_template = ['FolderSet']
168+
vcs_folders_template: List[Any] = ['FolderSet']
169169
for key, name, cmd in vcs:
170-
temp = ['List']
170+
temp: List[Any] = ['List']
171171
if key == 'rcs':
172172
# RCS uses multiple commands
173173
temp.extend([['File', key + '_bin_co', 'co', _('"co" command')],
@@ -196,8 +196,8 @@ def __init__(self, path):
196196
self.default_bool_prefs = self.bool_prefs.copy()
197197
self.default_int_prefs = self.int_prefs.copy()
198198
self.default_string_prefs = self.string_prefs.copy()
199+
199200
# load the user's preferences
200-
self.path = path
201201
if os.path.isfile(self.path):
202202
try:
203203
with open(self.path, 'r', encoding='utf-8') as f:
@@ -255,16 +255,16 @@ def _toggled_cb(self, widget, widgets, name):
255255

256256
# display the dialogue and update the preference values if the accept
257257
# button was pressed
258-
def runDialog(self, parent):
258+
def runDialog(self, parent: Gtk.Widget) -> None:
259259
dialog = Gtk.Dialog(_('Preferences'), parent=parent, destroy_with_parent=True)
260260
dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
261261
dialog.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
262262

263-
widgets = {}
263+
widgets: Dict[str, Gtk.Widget] = {}
264264
w = self._buildPrefsDialog(parent, widgets, self.template)
265265
# disable any preferences than are not relevant
266-
for k, v in self.disable_when.items():
267-
p, t = v
266+
for k, tuple_value in self.disable_when.items():
267+
p, t = tuple_value
268268
if widgets[p].get_active() == t:
269269
widgets[k].set_sensitive(False)
270270
dialog.vbox.add(w)
@@ -280,15 +280,15 @@ def runDialog(self, parent):
280280
self.string_prefs[k] = utils.null_to_empty(widgets[k].get_text())
281281
try:
282282
ss = []
283-
for k, v in self.bool_prefs.items():
284-
if v != self.default_bool_prefs[k]:
285-
ss.append(f'{k} {v}\n')
286-
for k, v in self.int_prefs.items():
287-
if v != self.default_int_prefs[k]:
288-
ss.append(f'{k} {v}\n')
289-
for k, v in self.string_prefs.items():
290-
if v != self.default_string_prefs[k]:
291-
v_escaped = v.replace('\\', '\\\\').replace('"', '\\"')
283+
for k, bool_value in self.bool_prefs.items():
284+
if bool_value != self.default_bool_prefs[k]:
285+
ss.append(f'{k} {bool_value}\n')
286+
for k, int_value in self.int_prefs.items():
287+
if int_value != self.default_int_prefs[k]:
288+
ss.append(f'{k} {int_value}\n')
289+
for k, str_value in self.string_prefs.items():
290+
if str_value != self.default_string_prefs[k]:
291+
v_escaped = str_value.replace('\\', '\\\\').replace('"', '\\"')
292292
ss.append(f'{k} "{v_escaped}"\n')
293293
ss.sort()
294294
with open(self.path, 'w', encoding='utf-8') as f:
@@ -384,7 +384,7 @@ def getString(self, name: str) -> str:
384384
def setString(self, name: str, value: str) -> None:
385385
self.string_prefs[name] = value
386386

387-
def getEncodings(self):
387+
def getEncodings(self) -> List[Optional[str]]:
388388
return self.encodings
389389

390390
def _getDefaultEncodings(self) -> List[str]:
@@ -439,7 +439,7 @@ def convertToNativePath(self, s: str) -> str:
439439

440440
# text entry widget with a button to help pick file names
441441
class _FileEntry(Gtk.Box):
442-
def __init__(self, parent, title):
442+
def __init__(self, parent: Gtk.Widget, title: str) -> None:
443443
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
444444
self.toplevel = parent
445445
self.title = title
@@ -456,7 +456,7 @@ def __init__(self, parent, title):
456456
button.show()
457457

458458
# action performed when the pick file button is pressed
459-
def chooseFile(self, widget):
459+
def chooseFile(self, widget: Gtk.Widget) -> None:
460460
dialog = Gtk.FileChooserDialog(
461461
self.title,
462462
self.toplevel,

0 commit comments

Comments
 (0)