Skip to content

Commit 4771736

Browse files
refactor: type hints and improvements in file_opener.py (#876)
* type fixes and minor improvements * remove `# noqa: N802`
1 parent 9d60c78 commit 4771736

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/tagstudio/qt/helpers/file_opener.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import sys
88
import traceback
99
from pathlib import Path
10+
from typing import override
1011

1112
import structlog
1213
from PySide6.QtCore import Qt
13-
from PySide6.QtWidgets import QLabel
14+
from PySide6.QtGui import QMouseEvent
15+
from PySide6.QtWidgets import QLabel, QWidget
1416

1517
from tagstudio.qt.helpers.silent_popen import silent_Popen
1618

@@ -115,36 +117,40 @@ def open_explorer(self):
115117

116118

117119
class FileOpenerLabel(QLabel):
118-
def __init__(self, parent=None):
120+
def __init__(self, parent: QWidget | None = None) -> None:
119121
"""Initialize the FileOpenerLabel.
120122
121123
Args:
122124
parent (QWidget, optional): The parent widget. Defaults to None.
123125
"""
126+
self.filepath: str | Path | None = None
127+
124128
super().__init__(parent)
125129

126-
def set_file_path(self, filepath):
130+
def set_file_path(self, filepath: str | Path) -> None:
127131
"""Set the filepath to open.
128132
129133
Args:
130134
filepath (str): The path to the file to open.
131135
"""
132136
self.filepath = filepath
133137

134-
def mousePressEvent(self, event): # noqa: N802
138+
@override
139+
def mousePressEvent(self, ev: QMouseEvent) -> None:
135140
"""Handle mouse press events.
136141
137142
On a left click, open the file in the default file explorer.
138143
On a right click, show a context menu.
139144
140145
Args:
141-
event (QMouseEvent): The mouse press event.
146+
ev (QMouseEvent): The mouse press event.
142147
"""
143-
super().mousePressEvent(event)
144-
145-
if event.button() == Qt.MouseButton.LeftButton:
148+
if ev.button() == Qt.MouseButton.LeftButton:
149+
assert self.filepath is not None, "File path is not set"
146150
opener = FileOpenerHelper(self.filepath)
147151
opener.open_explorer()
148-
elif event.button() == Qt.MouseButton.RightButton:
152+
elif ev.button() == Qt.MouseButton.RightButton:
149153
# Show context menu
150154
pass
155+
else:
156+
super().mousePressEvent(ev)

0 commit comments

Comments
 (0)