Skip to content

Commit

Permalink
Write to temp file in byte mode. Added extra test case for classic ma…
Browse files Browse the repository at this point in the history
…cOS line endings.
  • Loading branch information
freebird-2 committed Dec 7, 2024
1 parent c6fde72 commit 56e8408
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pylsp_mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

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

tmpFile: Optional[IO[str]] = None
tmpFile: Optional[IO[bytes]] = None

# In non-live-mode the file contents aren't updated.
# Returning an empty diagnostic clears the diagnostic result,
Expand Down Expand Up @@ -295,11 +295,11 @@ def get_diagnostics(
global tmpFile
if live_mode and not is_saved:
if tmpFile:
tmpFile = open(tmpFile.name, "w", encoding="utf-8", newline="")
tmpFile = open(tmpFile.name, "wb")
else:
tmpFile = tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8", newline="")
tmpFile = tempfile.NamedTemporaryFile("wb", delete=False)
log.info("live_mode tmpFile = %s", tmpFile.name)
tmpFile.write(document.source)
tmpFile.write(bytes(document.source, "utf-8"))
tmpFile.close()
args.extend(["--shadow-file", document.path, tmpFile.name])
elif not is_saved and document.path in last_diagnostics:
Expand Down
2 changes: 1 addition & 1 deletion test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_plugin(workspace, last_diagnostics_monkeypatch):
assert diag["code"] == "attr-defined"


@pytest.mark.parametrize("doc_source", ["a = 1\nb\n", "a = 1\r\nb\r\n"])
@pytest.mark.parametrize("doc_source", ["a = 1\nb\n", "a = 1\r\nb\r\n", "a = 1\rb\r"])
def test_handling_of_line_endings(workspace, last_diagnostics_monkeypatch, doc_source):
# setup
doc = Document(DOC_URI, workspace, doc_source)
Expand Down

0 comments on commit 56e8408

Please sign in to comment.