Skip to content

Commit c69647e

Browse files
authored
chore: add ANN rule to ruff (#251)
* update for uv * remove lock * update napari test * ignore failed to disconnect * better naming * bump min pyside * fix napari test * fix pyproject * fix rtd * chore: add ANN rule
1 parent 617752c commit c69647e

File tree

17 files changed

+67
-66
lines changed

17 files changed

+67
-66
lines changed

demo/model_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class MainWindow(QModelMainWindow):
12-
def __init__(self, app: Application):
12+
def __init__(self, app: Application) -> None:
1313
super().__init__(app)
1414

1515
self._cur_file: str = ""

demo/multi_file/functions.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
from qtpy.QtWidgets import QApplication, QFileDialog
22

33

4-
def open_file():
4+
def open_file() -> None:
55
name, _ = QFileDialog.getOpenFileName()
66
print("Open file:", name)
77

88

9-
def close():
9+
def close() -> None:
1010
QApplication.activeWindow().close()
1111
print("close")
1212

1313

14-
def undo():
14+
def undo() -> None:
1515
print("undo")
1616

1717

18-
def redo():
18+
def redo() -> None:
1919
print("redo")
2020

2121

22-
def cut():
22+
def cut() -> None:
2323
print("cut")
2424

2525

26-
def copy():
26+
def copy() -> None:
2727
print("copy")
2828

2929

30-
def paste():
30+
def paste() -> None:
3131
print("paste")

demo/qapplication.py

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

1818

1919
class MainWindow(QMainWindow):
20-
def __init__(self):
20+
def __init__(self) -> None:
2121
super().__init__()
2222

2323
self._cur_file = ""
@@ -32,28 +32,28 @@ def __init__(self):
3232

3333
self.set_current_file("")
3434

35-
def new_file(self):
35+
def new_file(self) -> None:
3636
if self.maybe_save():
3737
self._text_edit.clear()
3838
self.set_current_file("")
3939

40-
def open(self):
40+
def open(self) -> None:
4141
if self.maybe_save():
4242
fileName, filtr = QFileDialog.getOpenFileName(self)
4343
if fileName:
4444
self.load_file(fileName)
4545

46-
def save(self):
46+
def save(self) -> bool:
4747
return self.save_file(self._cur_file) if self._cur_file else self.save_as()
4848

49-
def save_as(self):
49+
def save_as(self) -> bool:
5050
fileName, filtr = QFileDialog.getSaveFileName(self)
5151
if fileName:
5252
return self.save_file(fileName)
5353

5454
return False
5555

56-
def about(self):
56+
def about(self) -> None:
5757
QMessageBox.about(
5858
self,
5959
"About Application",
@@ -62,7 +62,7 @@ def about(self):
6262
"toolbars, and a status bar.",
6363
)
6464

65-
def create_actions(self):
65+
def create_actions(self) -> None:
6666
self._new_act = QAction(
6767
fonticon.icon(FA6S.file_circle_plus),
6868
"&New",
@@ -145,7 +145,7 @@ def create_actions(self):
145145
self._text_edit.copyAvailable.connect(self._cut_act.setEnabled)
146146
self._text_edit.copyAvailable.connect(self._copy_act.setEnabled)
147147

148-
def create_menus(self):
148+
def create_menus(self) -> None:
149149
self._file_menu = self.menuBar().addMenu("&File")
150150
self._file_menu.addAction(self._new_act)
151151
self._file_menu.addAction(self._open_act)
@@ -164,7 +164,7 @@ def create_menus(self):
164164
self._help_menu = self.menuBar().addMenu("&Help")
165165
self._help_menu.addAction(self._about_act)
166166

167-
def create_tool_bars(self):
167+
def create_tool_bars(self) -> None:
168168
self._file_tool_bar = self.addToolBar("File")
169169
self._file_tool_bar.addAction(self._new_act)
170170
self._file_tool_bar.addAction(self._open_act)
@@ -175,10 +175,10 @@ def create_tool_bars(self):
175175
self._edit_tool_bar.addAction(self._copy_act)
176176
self._edit_tool_bar.addAction(self._paste_act)
177177

178-
def create_status_bar(self):
178+
def create_status_bar(self) -> None:
179179
self.statusBar().showMessage("Ready")
180180

181-
def maybe_save(self):
181+
def maybe_save(self) -> bool:
182182
if self._text_edit.document().isModified():
183183
ret = QMessageBox.warning(
184184
self,
@@ -194,7 +194,7 @@ def maybe_save(self):
194194
return False
195195
return True
196196

197-
def load_file(self, fileName):
197+
def load_file(self, fileName: str) -> None:
198198
file = QFile(fileName)
199199
if not file.open(QFile.OpenModeFlag.ReadOnly | QFile.OpenModeFlag.Text):
200200
reason = file.errorString()
@@ -211,7 +211,7 @@ def load_file(self, fileName):
211211
self.set_current_file(fileName)
212212
self.statusBar().showMessage("File loaded", 2000)
213213

214-
def save_file(self, fileName):
214+
def save_file(self, fileName: str) -> bool:
215215
error = None
216216
QApplication.setOverrideCursor(Qt.WaitCursor)
217217
file = QSaveFile(fileName)
@@ -234,7 +234,7 @@ def save_file(self, fileName):
234234
self.statusBar().showMessage("File saved", 2000)
235235
return True
236236

237-
def set_current_file(self, fileName: str):
237+
def set_current_file(self, fileName: str) -> None:
238238
self._cur_file = fileName
239239
self._text_edit.document().setModified(False)
240240
self.setWindowModified(False)
@@ -246,7 +246,7 @@ def set_current_file(self, fileName: str):
246246

247247
self.setWindowTitle(f"{shown_name}[*] - Application")
248248

249-
def stripped_name(self, fullFileName: str):
249+
def stripped_name(self, fullFileName: str) -> str:
250250
return QFileInfo(fullFileName).fileName()
251251

252252

pyproject.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,22 @@ select = [
117117
"C4", # flake8-comprehensions
118118
"B", # flake8-bugbear
119119
"A001", # flake8-builtins
120-
# "ANN", # flake8-annotations
121-
"RUF", # ruff-specific rules
122-
"TC", # flake8-type-checking
123-
"TID", # flake8-tidy-imports
120+
"ANN", # flake8-annotations
121+
"RUF", # ruff-specific rules
122+
"TC", # flake8-type-checking
123+
"TID", # flake8-tidy-imports
124124
]
125125
ignore = [
126-
"D401", # First line should be in imperative mood (remove to opt in)
126+
"D401", # First line should be in imperative mood (remove to opt in)
127+
"ANN401", # Disallow typing.Any
127128
]
128129

129130
[tool.ruff.lint.pyupgrade]
130131
# Preserve types, even if a file imports `from __future__ import annotations`.
131132
keep-runtime-typing = true
132133

133134
[tool.ruff.lint.per-file-ignores]
134-
"tests/*.py" = ["D", "E501"]
135+
"tests/*.py" = ["D", "E501", "ANN"]
135136
"demo/*" = ["D"]
136137
"docs/*" = ["D"]
137138
"src/app_model/_registries.py" = ["D10"]

src/app_model/backends/qt/_qaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(
4141
command_id: str,
4242
app: Application | str,
4343
parent: QObject | None = None,
44-
):
44+
) -> None:
4545
super().__init__(parent)
4646
self._app = Application.get_or_create(app) if isinstance(app, str) else app
4747
self._command_id = command_id
@@ -81,7 +81,7 @@ def __init__(
8181
parent: QObject | None = None,
8282
*,
8383
use_short_title: bool = False,
84-
):
84+
) -> None:
8585
super().__init__(command_rule.id, app, parent)
8686
self._cmd_rule = command_rule
8787
if use_short_title and command_rule.short_title:
@@ -146,7 +146,7 @@ def __init__(
146146
menu_item: MenuItem,
147147
app: Application | str,
148148
parent: QObject | None = None,
149-
):
149+
) -> None:
150150
super().__init__(menu_item.command, app, parent)
151151
self._menu_item = menu_item
152152
with contextlib.suppress(NameError):

src/app_model/backends/qt/_qmenu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
app: Application | str,
4343
title: str | None = None,
4444
parent: QWidget | None = None,
45-
):
45+
) -> None:
4646
QMenu.__init__(self, parent)
4747

4848
# NOTE: code duplication with QModelToolBar, but Qt mixins and multiple
@@ -132,7 +132,7 @@ def __init__(
132132
submenu: SubmenuItem,
133133
app: Application | str,
134134
parent: QWidget | None = None,
135-
):
135+
) -> None:
136136
assert isinstance(submenu, SubmenuItem), f"Expected str, got {type(submenu)!r}"
137137
self._submenu = submenu
138138
super().__init__(

src/app_model/expressions/_expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def __init__(
476476
op: ast.boolop,
477477
values: Sequence[ConstType | Expr],
478478
**kwargs: Unpack[_Attributes],
479-
):
479+
) -> None:
480480
super().__init__(op, [Expr._cast(v) for v in values], **kwargs)
481481

482482

src/app_model/types/_keys/_keybindings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class KeyBinding:
188188

189189
parts: list[SimpleKeyBinding] = Field(..., **MIN1) # type: ignore
190190

191-
def __init__(self, *, parts: list[SimpleKeyBinding]):
191+
def __init__(self, *, parts: list[SimpleKeyBinding]) -> None:
192192
self.parts = parts
193193

194194
def __str__(self) -> str:

tests/test_actions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def test_register_action_decorator(
4141
if mode == "decorator":
4242

4343
@register_action(app=app, id_or_action=cmd_id, **kwargs)
44-
def f1():
44+
def f1() -> str:
4545
return "hi"
4646

4747
assert f1() == "hi" # decorator returns the function
4848

4949
else:
5050

51-
def f2():
51+
def f2() -> str:
5252
return "hi"
5353

5454
if mode == "str":
@@ -99,14 +99,14 @@ def f2():
9999
assert not list(app.menus)
100100

101101

102-
def test_errors(simple_app: Application):
102+
def test_errors(simple_app: Application) -> None:
103103
with pytest.raises(ValueError, match="'title' is required"):
104104
simple_app.register_action("cmd_id") # type: ignore
105105
with pytest.raises(TypeError, match="must be a string or an Action"):
106106
simple_app.register_action(None) # type: ignore
107107

108108

109-
def test_register_multiple_actions(simple_app: Application):
109+
def test_register_multiple_actions(simple_app: Application) -> None:
110110
actions: list[Action] = [
111111
Action(id="cmd_id1", title="title1", callback=lambda: None),
112112
Action(id="cmd_id2", title="title2", callback=lambda: None),

tests/test_context/test_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from app_model.expressions._context import _OBJ_TO_CONTEXT
88

99

10-
def test_create_context():
10+
def test_create_context() -> None:
1111
"""You can create a context for any object"""
1212

1313
class T: ...
@@ -30,7 +30,7 @@ class T: ...
3030
create_context(T(), root={}) # type: ignore
3131

3232

33-
def test_create_and_get_scoped_contexts():
33+
def test_create_and_get_scoped_contexts() -> None:
3434
"""Test that objects created in the stack of another contexted object.
3535
3636
likely the most common way that this API will be used:
@@ -61,7 +61,7 @@ def __init__(self) -> None:
6161
assert len(_OBJ_TO_CONTEXT) == before
6262

6363

64-
def test_context_events():
64+
def test_context_events() -> None:
6565
"""Changing context keys emits an event"""
6666
mock = Mock()
6767
root = Context()

0 commit comments

Comments
 (0)