|
22 | 22 | import threading
|
23 | 23 | import getpass
|
24 | 24 |
|
| 25 | +import sublime |
25 | 26 | from SublimeLinter.lint import LintMatch, PermanentError, PythonLinter
|
26 | 27 |
|
27 | 28 |
|
|
33 | 34 | class TemporaryDirectory(Protocol):
|
34 | 35 | name = None # type: str
|
35 | 36 |
|
36 |
| - |
| 37 | +POSIX = sublime.platform() in ('osx', 'linux') |
| 38 | +BIN = 'bin' if POSIX else 'Scripts' |
37 | 39 | USER = getpass.getuser()
|
38 | 40 | TMPDIR_PREFIX = "SublimeLinter-contrib-mypy-%s" % USER
|
39 | 41 |
|
@@ -71,6 +73,8 @@ class Mypy(PythonLinter):
|
71 | 73 | "--show-error-codes": True,
|
72 | 74 | # Need this to silent lints for other files. Alternatively: 'skip'
|
73 | 75 | "--follow-imports": "silent",
|
| 76 | + # Automatically set "--python-executable" if `VIRTUAL_ENV` is set |
| 77 | + "set-python-executable-inside-venv": True, |
74 | 78 | }
|
75 | 79 |
|
76 | 80 | def cmd(self):
|
@@ -122,6 +126,16 @@ def cmd(self):
|
122 | 126 | return cmd
|
123 | 127 |
|
124 | 128 | def run(self, cmd, code):
|
| 129 | + if ( |
| 130 | + self.settings.get("set-python-executable-inside-venv") |
| 131 | + and (venv := self.get_environment().get("VIRTUAL_ENV")) |
| 132 | + and not cmd[0].startswith(venv) |
| 133 | + and not any(part.startswith("--python-executable") for part in cmd) |
| 134 | + and (py_executable := shutil.which("python", path=os.path.join(venv, BIN))) |
| 135 | + ): |
| 136 | + idx = cmd.index("--no-pretty") |
| 137 | + cmd = cmd[:idx] + ["--python-executable", py_executable] + cmd[idx:] |
| 138 | + |
125 | 139 | with locks[self.get_working_dir()]:
|
126 | 140 | return super().run(cmd, code)
|
127 | 141 |
|
|
0 commit comments