Skip to content

Commit 6136587

Browse files
freebird-2Richardk2n
authored andcommitted
Write to temp file in byte mode. Added extra test case for classic macOS line endings.
1 parent 1604463 commit 6136587

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

pylsp_mypy/plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
settingsCache: Dict[str, Dict[str, Any]] = {}
5353

54-
tmpFile: Optional[IO[str]] = None
54+
tmpFile: Optional[IO[bytes]] = None
5555

5656
# In non-live-mode the file contents aren't updated.
5757
# Returning an empty diagnostic clears the diagnostic result,
@@ -295,11 +295,11 @@ def get_diagnostics(
295295
global tmpFile
296296
if live_mode and not is_saved:
297297
if tmpFile:
298-
tmpFile = open(tmpFile.name, "w", encoding="utf-8", newline="")
298+
tmpFile = open(tmpFile.name, "wb")
299299
else:
300-
tmpFile = tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8", newline="")
300+
tmpFile = tempfile.NamedTemporaryFile("wb", delete=False)
301301
log.info("live_mode tmpFile = %s", tmpFile.name)
302-
tmpFile.write(document.source)
302+
tmpFile.write(bytes(document.source, "utf-8"))
303303
tmpFile.close()
304304
args.extend(["--shadow-file", document.path, tmpFile.name])
305305
elif not is_saved and document.path in last_diagnostics:

test/test_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_plugin(workspace, last_diagnostics_monkeypatch):
8383
assert diag["code"] == "attr-defined"
8484

8585

86-
@pytest.mark.parametrize("doc_source", ["a = 1\nb\n", "a = 1\r\nb\r\n"])
86+
@pytest.mark.parametrize("doc_source", ["a = 1\nb\n", "a = 1\r\nb\r\n", "a = 1\rb\r"])
8787
def test_handling_of_line_endings(workspace, last_diagnostics_monkeypatch, doc_source):
8888
# setup
8989
doc = Document(DOC_URI, workspace, doc_source)

0 commit comments

Comments
 (0)