Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Improve implementation and UI of console envs menu (IPython console/Main interpreter) #22200

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
get_home_dir, get_conf_path, get_module_path, running_in_ci,
running_in_ci_with_conda)
from spyder.config.manager import CONF
from spyder.config.utils import is_anaconda
from spyder.dependencies import DEPENDENCIES
from spyder.plugins.debugger.api import DebuggerWidgetActions
from spyder.plugins.externalterminal.api import ExtTerminalShConfiguration
Expand All @@ -84,6 +85,7 @@
WorkingDirSource, RunContext)
from spyder.py3compat import qbytearray_to_str, to_text_string
from spyder.utils.environ import set_user_env
from spyder.utils.conda import get_list_conda_envs
from spyder.utils.misc import remove_backslashes, rename_file
from spyder.utils.clipboard_helper import CLIPBOARD_HELPER
from spyder.utils.programs import find_program
Expand Down Expand Up @@ -3336,13 +3338,16 @@ def test_preferences_shortcut_reset_regression(main_window, qtbot):

@pytest.mark.order(1)
@flaky(max_runs=3)
@pytest.mark.skipif(not is_anaconda(), reason='Only works with Anaconda')
@pytest.mark.skipif(not running_in_ci(), reason='Only works on CIs')
def test_preferences_change_interpreter(qtbot, main_window):
"""Test that on main interpreter change signal is emitted."""
# Wait until the window is fully up
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(
lambda: shell.spyder_kernel_ready and shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)
timeout=SHELL_TIMEOUT,
)

# Check original pyls configuration
lsp = main_window.completions.get_provider('lsp')
Expand All @@ -3351,21 +3356,26 @@ def test_preferences_change_interpreter(qtbot, main_window):
assert jedi['environment'] is sys.executable
assert jedi['extra_paths'] == []

# Get conda env to use
conda_env = get_list_conda_envs()['Conda: jedi-test-env'][0]

# Change main interpreter on preferences
dlg, index, page = preferences_dialog_helper(qtbot, main_window,
'main_interpreter')
dlg, index, page = preferences_dialog_helper(
qtbot, main_window, 'main_interpreter'
)
page.cus_exec_radio.radiobutton.setChecked(True)
page.cus_exec_combo.combobox.setCurrentText(sys.executable)
page.cus_exec_combo.combobox.setCurrentText(conda_env)

mi_container = main_window.main_interpreter.get_container()
with qtbot.waitSignal(mi_container.sig_interpreter_changed,
timeout=5000, raising=True):
with qtbot.waitSignal(
mi_container.sig_interpreter_changed, timeout=5000, raising=True
):
dlg.ok_btn.animateClick()

# Check updated pyls configuration
config = lsp.generate_python_config()
jedi = config['configurations']['pylsp']['plugins']['jedi']
assert jedi['environment'] == sys.executable
assert jedi['environment'] == conda_env
assert jedi['extra_paths'] == []


Expand Down
2 changes: 2 additions & 0 deletions spyder/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
'umr/namelist': [],
'custom_interpreters_list': [],
'custom_interpreter': '',
'last_envs': {}
}),
('ipython_console',
{
Expand Down Expand Up @@ -615,6 +616,7 @@
'custom_interpreters_list',
'custom_interpreter',
'executable',
'last_envs',
]
),
('onlinehelp', [
Expand Down
6 changes: 2 additions & 4 deletions spyder/plugins/completion/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import functools
import inspect
import logging
import os
from typing import List, Union
import weakref

Expand All @@ -25,11 +24,10 @@
from qtpy.QtCore import QRecursiveMutex, QMutexLocker, QTimer, Slot, Signal

# Local imports
from spyder.config.manager import CONF
from spyder.api.plugins import SpyderPluginV2, Plugins
from spyder.api.plugin_registration.decorators import (
on_plugin_available, on_plugin_teardown)
from spyder.config.base import _, running_under_pytest
from spyder.api.translations import _
from spyder.config.user import NoDefault
from spyder.plugins.completion.api import (CompletionRequestTypes,
SpyderCompletionProvider,
Expand Down Expand Up @@ -492,7 +490,7 @@ def update_completion_status(self):
mi_status = maininterpreter.get_container().interpreter_status

value = mi_status.value
tool_tip = mi_status._interpreter
tool_tip = maininterpreter.get_container()._interpreter

if '(' in value:
value = value.split('(')[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ def test_completions_environment(completions_codeeditor, qtbot, tmpdir):
code_editor.toggle_code_snippets(False)

# Get jedi test env
py_exe = get_list_conda_envs()['conda: jedi-test-env'][0]
py_exe = get_list_conda_envs()['Conda: jedi-test-env'][0]
assert os.path.isfile(py_exe)

# Check that we can't code complete Flask in the default interpreter
Expand Down
8 changes: 8 additions & 0 deletions spyder/plugins/ipythonconsole/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,11 @@ class IPythonConsoleWidgetCornerWidgets:
ResetButton = "reset_button"
InterruptButton = "interrupt_button"
TimeElapsedLabel = "time_elapsed_label"


class EnvironmentConsolesMenuSections:
Default = "default_section"
Conda = "conda_section"
Pyenv = "pyenv_section"
Custom = "custom_section"
Other = "other_section"
14 changes: 14 additions & 0 deletions spyder/plugins/ipythonconsole/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class IPythonConsole(SpyderDockablePlugin, RunExecutor):
OPTIONAL = [
Plugins.Editor,
Plugins.History,
Plugins.MainInterpreter,
Plugins.MainMenu,
Plugins.Run,
Plugins.Projects,
Expand Down Expand Up @@ -449,6 +450,11 @@ def on_remote_client_available(self):
remote_client.sig_server_stopped.connect(self._close_remote_clients)
remote_client.sig_server_renamed.connect(self._rename_remote_clients)

@on_plugin_available(plugin=Plugins.MainInterpreter)
def on_main_interpreter_available(self):
main_interpreter = self.get_plugin(Plugins.MainInterpreter)
main_interpreter.sig_environments_updated.connect(self._update_envs)

@on_plugin_teardown(plugin=Plugins.Preferences)
def on_preferences_teardown(self):
# Register conf page
Expand Down Expand Up @@ -510,6 +516,11 @@ def on_remote_client_teardown(self):
self._rename_remote_clients
)

@on_plugin_teardown(plugin=Plugins.MainInterpreter)
def on_main_interpreter_teardown(self):
main_interpreter = self.get_plugin(Plugins.MainInterpreter)
main_interpreter.sig_environments_updated.disconnect(self._update_envs)

def update_font(self):
"""Update font from Preferences"""
font = self.get_font(SpyderFontType.Monospace)
Expand Down Expand Up @@ -538,6 +549,9 @@ def _close_remote_clients(self, server_id):
def _rename_remote_clients(self, server_id):
self.get_widget().rename_remote_clients(server_id)

def _update_envs(self, envs):
self.get_widget().update_envs(envs)

# ---- Public API
# -------------------------------------------------------------------------

Expand Down
3 changes: 1 addition & 2 deletions spyder/plugins/ipythonconsole/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import os
import os.path as osp
import sys
import tempfile
import threading
import traceback
from unittest.mock import Mock
Expand Down Expand Up @@ -74,7 +73,7 @@ def get_conda_test_env():
its executable.
"""
# Get conda env to use
test_env_executable = get_list_conda_envs()['conda: spytest-ž'][0]
test_env_executable = get_list_conda_envs()['Conda: spytest-ž'][0]

# Get the env prefix
if os.name == 'nt':
Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -2317,7 +2317,7 @@ def test_show_spyder_kernels_error_on_restart(ipyconsole, qtbot):

# Point to an interpreter without Spyder-kernels
ipyconsole.set_conf('default', False, section='main_interpreter')
pyexec = get_list_conda_envs()['conda: base'][0]
pyexec = get_list_conda_envs()['Conda: base'][0]
ipyconsole.set_conf('executable', pyexec, section='main_interpreter')

# Restart kernel
Expand Down
Loading