Skip to content

Commit

Permalink
pylint: add the pythonpath environment from pythonpath-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
znapy committed Mar 14, 2024
1 parent caf3010 commit c654d8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
16 changes: 13 additions & 3 deletions spyder/plugins/pylint/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
33 changes: 23 additions & 10 deletions spyder/plugins/pylint/tests/test_pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Standard library imports
from io import open
import os
import os.path as osp
from unittest.mock import Mock, MagicMock

Expand Down Expand Up @@ -65,6 +66,7 @@
good-names=e
"""


class MainWindowMock(QMainWindow):
sig_editor_focus_changed = Signal(str)

Expand Down Expand Up @@ -333,28 +335,39 @@ 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",
return_value=False)
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__":
Expand Down

0 comments on commit c654d8e

Please sign in to comment.