Skip to content

Commit

Permalink
Merge pull request spyder-ide#21891 from znapy/pypath4pylint
Browse files Browse the repository at this point in the history
PR: Pass paths from the Pythonpath manager to the Pylint plugin
  • Loading branch information
ccordoba12 authored Nov 28, 2024
2 parents 9eb4863 + ec7d5e4 commit a642045
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
49 changes: 34 additions & 15 deletions spyder/plugins/pylint/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,21 +373,12 @@ def _start(self):
lambda ec, es=QProcess.ExitStatus: self._finished(ec, es))

command_args = self.get_command(self.get_filename())
processEnvironment = QProcessEnvironment()
processEnvironment.insert("PYTHONIOENCODING", "utf8")

if os.name == 'nt':
# Needed due to changes in Pylint 2.14.0
# See spyder-ide/spyder#18175
home_dir = get_home_dir()
user_profile = os.environ.get("USERPROFILE", home_dir)
processEnvironment.insert("USERPROFILE", user_profile)
# Needed for Windows installations using standalone Python and pip.
# See spyder-ide/spyder#19385
if not is_conda_env(sys.prefix):
processEnvironment.insert("APPDATA", os.environ.get("APPDATA"))

process.setProcessEnvironment(processEnvironment)
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 @@ -946,6 +937,34 @@ def get_command(self, filename):
command_args.append(filename)
return command_args

@staticmethod
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
home_dir = get_home_dir()
user_profile = os.environ.get("USERPROFILE", home_dir)
process_environment.insert("USERPROFILE", user_profile)
# Needed for Windows installations using standalone Python and pip.
# See spyder-ide/spyder#19385
if not is_conda_env(sys.prefix):
process_environment.insert(
"APPDATA", os.environ.get("APPDATA")
)

return process_environment

def parse_output(self, output):
"""
Parse output and return current revious rate and results.
Expand Down
25 changes: 25 additions & 0 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
import sys
from unittest.mock import Mock, MagicMock
Expand Down Expand Up @@ -335,5 +336,29 @@ def test_custom_interpreter(pylint_plugin, tmp_path, qtbot,
assert errors


def test_get_environment(mocker):
"""Test that the environment variables depend on the OS."""
if os.name == 'nt':
mocker.patch(
"spyder.plugins.pylint.main_widget.is_conda_env",
return_value=False
)
mocker.patch(
"spyder.plugins.pylint.main_widget.get_home_dir",
return_value=''
)

expected_vars = {
"nt": ["APPDATA", "PYTHONIOENCODING", "PYTHONPATH", "USERPROFILE"],
"posix": ["PYTHONIOENCODING", "PYTHONPATH"]
}

process_environment = Pylint.WIDGET_CLASS.get_environment(
pythonpath_manager_values=["project_dir"]
)

assert process_environment.keys() == expected_vars[os.name]


if __name__ == "__main__":
pytest.main([osp.basename(__file__), '-vv', '-rw'])

0 comments on commit a642045

Please sign in to comment.