Skip to content

Commit

Permalink
[python] Send LSP diagnostic warnings to logs (#2953)
Browse files Browse the repository at this point in the history
* escape sequence warnings to logs

* context manager around lsp diagnostics

* linting

* filter _all_ warnings
  • Loading branch information
isabelizimm authored May 6, 2024
1 parent 18cb861 commit 2e3827d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,6 @@ def _showwarning(self, message, category, filename, lineno, file=None, line=None
# if coming from one of our files, log and don't send to user
positron_files_path = Path(__file__).parent

if str(positron_files_path) in str(filename):
msg = f"{filename}-{lineno}: {category}: {message}"
logger.warning(msg)
return

# Check if the filename refers to a cell in the Positron Console.
# We use the fact that ipykernel sets the filename to a path starting in the root temporary
# directory. We can't determine the full filename since it depends on the cell's code which
Expand All @@ -490,6 +485,14 @@ def _showwarning(self, message, category, filename, lineno, file=None, line=None
if console_dir in str(filename):
filename = f"<positron-console-cell-{self.execution_count}>"

# send to logs if warning is coming from Positron files
# also send warnings from attempted compiles from IPython to logs
# https://github.com/ipython/ipython/blob/8.24.0/IPython/core/async_helpers.py#L151
if str(positron_files_path) in str(filename) or str(filename) == "<>":
msg = f"{filename}-{lineno}: {category}: {message}"
logger.warning(msg)
return

msg = warnings.WarningMessage(message, category, filename, lineno, file, line)

return original_showwarning(message, category, filename, lineno, file, line) # type: ignore reportAttributeAccessIssue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import re
import threading
import warnings
from functools import lru_cache
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Type, Union, cast

Expand Down Expand Up @@ -670,7 +671,9 @@ def _publish_diagnostics(server: JediLanguageServer, uri: str) -> None:

# --- Start Positron ---
try:
diagnostic = jedi_utils.lsp_python_diagnostic(uri, doc.source)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
diagnostic = jedi_utils.lsp_python_diagnostic(uri, doc.source)
except Exception:
logger.exception(f"Failed to publish diagnostics for uri {uri}", exc_info=True)
diagnostic = None
Expand Down

0 comments on commit 2e3827d

Please sign in to comment.