From c654d8e8615a9a28e907595386a1b03440912a97 Mon Sep 17 00:00:00 2001 From: alic <1570970+Znapy@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:40:35 +0000 Subject: [PATCH] pylint: add the pythonpath environment from pythonpath-manager --- spyder/plugins/pylint/main_widget.py | 16 +++++++++-- spyder/plugins/pylint/tests/test_pylint.py | 33 +++++++++++++++------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/spyder/plugins/pylint/main_widget.py b/spyder/plugins/pylint/main_widget.py index 6911af8e30a..d7e8b173034 100644 --- a/spyder/plugins/pylint/main_widget.py +++ b/spyder/plugins/pylint/main_widget.py @@ -373,7 +373,10 @@ def _start(self): lambda ec, es=QProcess.ExitStatus: self._finished(ec, es)) command_args = self.get_command(self.get_filename()) - process.setProcessEnvironment(self.get_environment()) + pythonpath_manager_values = self.get_conf( + 'spyder_pythonpath', default=[], section='pythonpath_manager') + process.setProcessEnvironment( + self.get_environment(pythonpath_manager_values)) process.start(sys.executable, command_args) running = process.waitForStarted() if not running: @@ -933,11 +936,17 @@ def get_command(self, filename): return command_args @staticmethod - def get_environment() -> QProcessEnvironment: + def get_environment(pythonpath_manager_values: list + ) -> QProcessEnvironment: """Get evironment variables for pylint command.""" process_environment = QProcessEnvironment() process_environment.insert("PYTHONIOENCODING", "utf8") + if pythonpath_manager_values: + pypath = os.pathsep.join(pythonpath_manager_values) + # See PR spyder-ide/spyder#21891 + process_environment.insert("PYTHONPATH", pypath) + if os.name == 'nt': # Needed due to changes in Pylint 2.14.0 # See spyder-ide/spyder#18175 @@ -947,7 +956,8 @@ def get_environment() -> QProcessEnvironment: # Needed for Windows installations using standalone Python and pip. # See spyder-ide/spyder#19385 if not is_conda_based_app() and not is_anaconda(): - process_environment.insert("APPDATA", os.environ.get("APPDATA")) + process_environment.insert("APPDATA", + os.environ.get("APPDATA")) return process_environment diff --git a/spyder/plugins/pylint/tests/test_pylint.py b/spyder/plugins/pylint/tests/test_pylint.py index 9ad21068ac7..28de1d09f33 100644 --- a/spyder/plugins/pylint/tests/test_pylint.py +++ b/spyder/plugins/pylint/tests/test_pylint.py @@ -9,6 +9,7 @@ # Standard library imports from io import open +import os import os.path as osp from unittest.mock import Mock, MagicMock @@ -65,6 +66,7 @@ good-names=e """ + class MainWindowMock(QMainWindow): sig_editor_focus_changed = Signal(str) @@ -333,8 +335,8 @@ def test_custom_interpreter(pylint_plugin, tmp_path, qtbot, def test_get_environment_windows(mocker): """Test that the environment variables match the Windows OS.""" - mocker.patch("spyder.plugins.pylint.main_widget.os.name", - "nt") + os_name_original = os.name + mocker.patch("spyder.plugins.pylint.main_widget.os.name", "nt") mocker.patch("spyder.plugins.pylint.main_widget.is_conda_based_app", return_value=False) mocker.patch("spyder.plugins.pylint.main_widget.is_anaconda", @@ -342,19 +344,30 @@ def test_get_environment_windows(mocker): mocker.patch("spyder.plugins.pylint.main_widget.get_home_dir", return_value='') - process_environment = Pylint.WIDGET_CLASS.get_environment() + process_environment = Pylint.WIDGET_CLASS.get_environment( + pythonpath_manager_values=["project_dir"]) - assert process_environment.keys() ==\ - ["APPDATA", "PYTHONIOENCODING", "USERPROFILE"] + try: + assert sorted(process_environment.keys()) ==\ + ["APPDATA", "PYTHONIOENCODING", "PYTHONPATH", "USERPROFILE"] + finally: + mocker.patch("spyder.plugins.pylint.main_widget.os.name", + os_name_original) def test_get_environment_nonwindows(mocker): """Test that the environment variables match the Linux and Mac OS.""" - mocker.patch("spyder.plugins.pylint.main_widget.os.name", - "posix") - process_environment = Pylint.WIDGET_CLASS.get_environment() - - assert process_environment.keys() == ["PYTHONIOENCODING"] + os_name_original = os.name + mocker.patch("spyder.plugins.pylint.main_widget.os.name", "posix") + process_environment = Pylint.WIDGET_CLASS.get_environment( + pythonpath_manager_values=["project_dir"]) + + try: + assert sorted(process_environment.keys()) == \ + ["PYTHONIOENCODING", "PYTHONPATH"] + finally: + mocker.patch("spyder.plugins.pylint.main_widget.os.name", + os_name_original) if __name__ == "__main__":