Skip to content

Commit fc470b9

Browse files
committed
Merge from 4.x: PR spyder-ide#12884
Fixes spyder-ide#12883
2 parents 9dd10e7 + d7f5eab commit fc470b9

4 files changed

Lines changed: 93 additions & 27 deletions

File tree

spyder/plugins/pylint/confpage.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
from spyder.plugins.pylint.widgets.pylintgui import PylintWidget
1717

1818

19+
MIN_HISTORY_ENTRIES = 5
20+
MAX_HISTORY_ENTRIES = 100
21+
DEFAULT_HISTORY_ENTRIES = 30
22+
23+
1924
class PylintConfigPage(PluginConfigPage):
2025
def setup_page(self):
2126
settings_group = QGroupBox(_("Settings"))
@@ -28,8 +33,8 @@ def setup_page(self):
2833
hist_label1.setWordWrap(True)
2934
hist_spin = self.create_spinbox(
3035
_("History: "),
31-
_(" results"), 'max_entries', default=50,
32-
min_=10, max_=1000000, step=10)
36+
_(" results"), 'max_entries', default=DEFAULT_HISTORY_ENTRIES,
37+
min_=MIN_HISTORY_ENTRIES, max_=MAX_HISTORY_ENTRIES, step=1)
3338

3439
results_group = QGroupBox(_("Results"))
3540
results_label1 = QLabel(_("Results are stored here:"))

spyder/plugins/pylint/plugin.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
from spyder.utils import icon_manager as ima
2727
from spyder.utils.programs import is_module_installed
2828
from spyder.utils.qthelpers import create_action, MENU_SEPARATOR
29-
from spyder.plugins.pylint.confpage import PylintConfigPage
29+
from spyder.plugins.pylint.confpage import (PylintConfigPage,
30+
MAX_HISTORY_ENTRIES,
31+
MIN_HISTORY_ENTRIES,
32+
DEFAULT_HISTORY_ENTRIES)
3033
from spyder.plugins.pylint.widgets.pylintgui import PylintWidget
3134

3235

@@ -49,7 +52,7 @@ class Pylint(SpyderPluginWidget):
4952
def __init__(self, parent=None):
5053
super().__init__(parent)
5154

52-
max_entries = self.get_option('max_entries', 50)
55+
max_entries = self.get_option('max_entries', DEFAULT_HISTORY_ENTRIES)
5356
self.pylint = PylintWidget(self, max_entries=max_entries,
5457
options_button=self.options_button,
5558
text_color=MAIN_TEXT_COLOR,
@@ -123,17 +126,20 @@ def apply_plugin_settings(self, options):
123126
"""Apply configuration file's plugin settings"""
124127
# The history depth option will be applied at
125128
# next Spyder startup, which is soon enough
126-
pass
129+
self.pylint.change_history_limit(self.get_option('max_entries'))
127130

128131
# ----- Public API --------------------------------------------------------
129132
@Slot()
130133
def change_history_depth(self):
131134
"Change history max entries"""
132135
depth, valid = QInputDialog.getInt(
133136
self, _('History'), _('Maximum entries'),
134-
self.get_option('max_entries'), 10, 10000)
137+
self.get_option('max_entries'),
138+
MIN_HISTORY_ENTRIES,
139+
MAX_HISTORY_ENTRIES)
135140
if valid:
136141
self.set_option('max_entries', depth)
142+
self.pylint.change_history_limit(depth)
137143

138144
def get_filename(self):
139145
"""Get current filename in combobox."""

spyder/plugins/pylint/tests/test_pylint.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,26 @@ def pylint_test_script(pylintrc_search_paths):
7777
with open(script_path, mode="w",
7878
encoding="utf-8", newline="\n") as script_file:
7979
script_file.write(PYLINT_TEST_SCRIPT)
80+
8081
return script_path
8182

8283

84+
@pytest.fixture
85+
def pylint_test_scripts(pylintrc_search_paths):
86+
def _pylint_test_scripts(filenames):
87+
"""Write scripts for testing Pylint to a temporary directory."""
88+
script_paths = []
89+
for filename in filenames:
90+
script_path = osp.join(
91+
pylintrc_search_paths[SCRIPT_DIR], filename)
92+
with open(script_path, mode="w",
93+
encoding="utf-8", newline="\n") as script_file:
94+
script_file.write(PYLINT_TEST_SCRIPT)
95+
script_paths.append(script_path)
96+
return script_paths
97+
return _pylint_test_scripts
98+
99+
83100
@pytest.fixture(
84101
params=[
85102
[], [SCRIPT_DIR], [WORKING_DIR], [PROJECT_DIR], [HOME_DIR],
@@ -175,33 +192,47 @@ def test_pylint_widget_pylintrc(
175192
for bad_name in bad_names])
176193

177194

178-
def test_pylint_max_history_conf(pylint_test_script, mocker, qtbot):
195+
def test_pylint_max_history_conf(pylint_test_scripts, mocker):
179196
"""Regression test for checking max_entries configuration.
180197
181-
For further information see spyder-ide/spyder#12874
198+
For further information see spyder-ide/spyder#12884
182199
"""
183200
# Create the pylint widget for code analysis
184201
main_window = MainWindowMock()
185202
main_window.projects.get_active_project_path = mocker.MagicMock(
186203
return_value=None)
187204
pylint_sw = Pylint(parent=main_window)
188205
pylint_widget = PylintWidget(parent=pylint_sw)
206+
pylint_widget.filecombo.clear()
207+
208+
script_0, script_1, script_2 = pylint_test_scripts(
209+
["test_script_{}.py".format(n) for n in range(3)])
189210

190211
# Change the max_entry to 2
191212
pylint_widget.parent.set_option('max_entries', 2)
213+
pylint_widget.change_history_limit(2)
192214
assert pylint_widget.parent.get_option('max_entries') == 2
193215

194-
# Analyze the test script with 1 as max_entry
195-
pylint_widget.analyze(filename=pylint_test_script)
196-
qtbot.waitUntil(
197-
lambda: pylint_widget.get_data(pylint_test_script)[1] is not None,
198-
timeout=5000)
199-
pylint_data = pylint_widget.get_data(filename=pylint_test_script)
200-
results = pylint_data[1][-1]
216+
# Call to set_filename
217+
pylint_widget.set_filename(filename=script_0)
218+
assert pylint_widget.filecombo.count() == 1
219+
220+
# Add to more filenames
221+
pylint_widget.set_filename(filename=script_1)
222+
pylint_widget.set_filename(filename=script_2)
223+
224+
assert pylint_widget.filecombo.count() == 2
225+
226+
assert 'test_script_2.py' in pylint_widget.curr_filenames[0]
227+
assert 'test_script_1.py' in pylint_widget.curr_filenames[1]
228+
229+
# Change the max entry to 1
230+
pylint_widget.parent.set_option('max_entries', 1)
231+
pylint_widget.change_history_limit(1)
232+
233+
assert pylint_widget.filecombo.count() == 1
201234

202-
max_entries = pylint_widget.parent.get_option('max_entries')
203-
for key in results:
204-
assert len(results[key]) <= max_entries
235+
assert 'test_script_2.py' in pylint_widget.curr_filenames[0]
205236

206237

207238
if __name__ == "__main__":

spyder/plugins/pylint/widgets/pylintgui.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def __init__(self, parent, max_entries=100, options_button=None,
170170
self.output = None
171171
self.error_output = None
172172
self.filename = None
173+
self.curr_filenames = []
173174
self.text_color = text_color
174175
self.prevrate_color = prevrate_color
175176

@@ -184,6 +185,7 @@ def __init__(self, parent, max_entries=100, options_button=None,
184185
pass
185186

186187
self.filecombo = PythonModulesComboBox(self)
188+
self.filecombo.setInsertPolicy(self.filecombo.InsertAtTop)
187189

188190
self.start_button = create_toolbutton(
189191
self,
@@ -245,7 +247,7 @@ def __init__(self, parent, max_entries=100, options_button=None,
245247

246248
if self.rdata:
247249
self.remove_obsolete_items()
248-
self.filecombo.addItems(self.get_filenames())
250+
self.filecombo.insertItems(0, self.get_filenames())
249251
self.start_button.setEnabled(self.filecombo.is_valid())
250252
else:
251253
self.start_button.setEnabled(False)
@@ -266,13 +268,38 @@ def set_filename(self, filename):
266268
filename = str(filename) # filename is a QString instance
267269
self.kill_if_running()
268270
index, _data = self.get_data(filename)
269-
if index is None:
270-
self.filecombo.addItem(filename)
271-
self.filecombo.setCurrentIndex(self.filecombo.count()-1)
271+
is_parent = self.parent is not None
272+
273+
if filename not in self.curr_filenames:
274+
self.filecombo.insertItem(0, filename)
275+
self.curr_filenames.insert(0, filename)
276+
self.filecombo.setCurrentIndex(0)
272277
else:
273-
self.filecombo.setCurrentIndex(self.filecombo.findText(filename))
278+
index = self.filecombo.findText(filename)
279+
self.filecombo.removeItem(index)
280+
self.curr_filenames.pop(index)
281+
self.filecombo.insertItem(0, filename)
282+
self.curr_filenames.insert(0, filename)
283+
self.filecombo.setCurrentIndex(0)
284+
285+
num_elements = self.filecombo.count()
286+
if is_parent and (num_elements >
287+
self.parent.get_option('max_entries')):
288+
self.filecombo.removeItem(num_elements - 1)
289+
self.curr_filenames.pop(num_elements - 1)
274290
self.filecombo.selected()
275291

292+
def change_history_limit(self, new_limit):
293+
"""Change the number of files listed in the history combobox."""
294+
if self.filecombo.count() > new_limit:
295+
num_elements = self.filecombo.count()
296+
diff = num_elements - new_limit
297+
for __ in range(diff):
298+
num_elements = self.filecombo.count()
299+
self.filecombo.removeItem(num_elements - 1)
300+
self.curr_filenames.pop(num_elements - 1)
301+
self.filecombo.selected()
302+
276303
def analyze(self, filename=None):
277304
"""
278305
Perform code analysis for given `filename`.
@@ -464,10 +491,7 @@ def finished(self, exit_code, exit_status):
464491
else:
465492
pylint_item = (module, items["line_nb"], items["message"],
466493
items["msg_id"], items["message_name"])
467-
act_result = results[line[0] + ':']
468-
if (self.parent is not None and
469-
len(act_result) < self.parent.get_option('max_entries')):
470-
results[line[0] + ':'].append(pylint_item)
494+
results[line[0] + ':'].append(pylint_item)
471495

472496
# Rate
473497
rate = None

0 commit comments

Comments
 (0)