Skip to content

Commit 86ca6de

Browse files
committed
fixup! Add option to override command line
1 parent 8e3a591 commit 86ca6de

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Configuration
3333
``strict`` (default is False) refers to the ``strict`` option of ``mypy``.
3434
This option often is too strict to be useful.
3535

36-
``overrides`` (default is ``[]``) specifies a list of alternate or supplemental command-line options.
37-
This modifies the options passed to ``mypy`` or the mypy-specific ones passed to ``dmypy run``. When present, the special boolean member ``True`` is replaced with the command-line options that would've been passed had ``overrides`` not been specified.
36+
``overrides`` (default is ``[True]``) specifies a list of alternate or supplemental command-line options.
37+
This modifies the options passed to ``mypy`` or the mypy-specific ones passed to ``dmypy run``. When present, the special boolean member ``True`` is replaced with the command-line options that would've been passed had ``overrides`` not been specified. Later options take precedence, which allows for replacing or negating individual default options (see ``mypy.main:process_options`` and ``mypy --help | grep inverse``).
3838

3939
Depending on your editor, the configuration (found in a file called pylsp-mypy.cfg in your workspace or a parent directory) should be roughly like this for a standard configuration:
4040

pylsp_mypy/plugin.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pylsp import hookimpl
1717
from pylsp.workspace import Document, Workspace
1818
from pylsp.config.config import Config
19-
from typing import Optional, Dict, Any, IO, List, Generator
19+
from typing import Optional, Dict, Any, IO, List
2020
import atexit
2121
import collections
2222
import warnings
@@ -95,13 +95,13 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[
9595
return None
9696

9797

98-
def apply_overrides(args: List[str], overrides: List[Any]) -> Generator[str, None, None]:
99-
"""Replace or combine overrides with command-line args."""
100-
for v in overrides:
101-
if v is True:
102-
yield from iter(args)
103-
continue
104-
yield v
98+
def apply_overrides(args: List[str], overrides: List[Any]) -> List[str]:
99+
"""Replace or combine default command-line options with overrides."""
100+
o = iter(overrides)
101+
if True not in o:
102+
return overrides
103+
rest = list(o)
104+
return [*overrides[: -(len(rest) + 1)], *args, *rest]
105105

106106

107107
@hookimpl
@@ -200,7 +200,7 @@ def pylsp_lint(
200200
if not dmypy:
201201
args.extend(["--incremental", "--follow-imports", "silent"])
202202
if overrides:
203-
args = list(apply_overrides(args, overrides))
203+
args = apply_overrides(args, overrides)
204204

205205
log.info("executing mypy args = %s", args)
206206
completed_process = subprocess.run(
@@ -222,7 +222,7 @@ def pylsp_lint(
222222
subprocess.run(["dmypy", "kill"])
223223

224224
# run to use existing daemon or restart if required
225-
args = ["run", "--"] + (list(apply_overrides(args, overrides)) if overrides else args)
225+
args = ["run", "--"] + (apply_overrides(args, overrides) if overrides else args)
226226
log.info("dmypy run args = %s", args)
227227
completed_process = subprocess.run(
228228
["dmypy", *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE

test/test_plugin.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,25 @@ def foo():
129129

130130

131131
def test_apply_overrides():
132-
assert list(plugin.apply_overrides(["1", "2"], [])) == []
133-
assert list(plugin.apply_overrides(["1", "2"], ["a"])) == ["a"]
134-
assert list(plugin.apply_overrides(["1", "2"], ["a", True])) == ["a", "1", "2"]
135-
assert list(plugin.apply_overrides(["1", "2"], [True, "a"])) == ["1", "2", "a"]
136-
assert list(plugin.apply_overrides(["1"], ["a", True, "b"])) == ["a", "1", "b"]
132+
assert plugin.apply_overrides(["1", "2"], []) == []
133+
assert plugin.apply_overrides(["1", "2"], ["a"]) == ["a"]
134+
assert plugin.apply_overrides(["1", "2"], ["a", True]) == ["a", "1", "2"]
135+
assert plugin.apply_overrides(["1", "2"], [True, "a"]) == ["1", "2", "a"]
136+
assert plugin.apply_overrides(["1"], ["a", True, "b"]) == ["a", "1", "b"]
137137

138138

139139
def test_option_overrides(tmpdir, diag_mp, workspace):
140140
import sys
141-
from textwrap import dedent
142141

143142
sentinel = tmpdir / "ran"
144143

145-
source = dedent(
146-
"""\
147-
#!{}
148-
import os, sys, pathlib
149-
pathlib.Path({!r}).touch()
150-
os.execv({!r}, sys.argv)
151-
"""
152-
).format(sys.executable, str(sentinel), sys.executable)
144+
source = """\
145+
#!{}
146+
import os, sys, pathlib
147+
pathlib.Path({!r}).touch()
148+
os.execv({!r}, sys.argv)\n"""
149+
150+
source = source.format(sys.executable, str(sentinel), sys.executable)
153151

154152
wrapper = tmpdir / "bin/wrapper"
155153
wrapper.write(source, ensure=True)

0 commit comments

Comments
 (0)