Skip to content

Send escape sequence SyntaxWarning to logs#2953

Merged
isabelizimm merged 4 commits into
mainfrom
silence-syntaxwarning
May 6, 2024
Merged

Send escape sequence SyntaxWarning to logs#2953
isabelizimm merged 4 commits into
mainfrom
silence-syntaxwarning

Conversation

@isabelizimm

Copy link
Copy Markdown
Contributor

Intent

Describe briefly what problem this pull request resolves, or what new feature it introduces. Include screenshots of any new or altered UI. Link to any GitHub issues that are related.

addresses #2615 by sending SyntaxWarnings about escape sequences, that can be populated at compile time, to logs

Approach

Describe the approach taken and the tradeoffs involved if non-obvious; add an overview of the solution if it's complicated.

Builds on infrastructure set up in #2776

QA Notes

Add additional information for QA on how to validate the change, paying special attention to the level of risk, adjacent areas that could be affected by the change, and any important contextual information not present in the linked issues.

I've been testing with

import warnings
warnings.warn("invalid escape sequence", SyntaxWarning)

since the example from #2615 is not reproducible for me.

@jonvanausdeln

Copy link
Copy Markdown
Contributor

I just tried the branch on windows, and it does appear to fix #2615 !

@isabelizimm isabelizimm requested a review from seeM May 1, 2024 16:09
@seeM

seeM commented May 2, 2024

Copy link
Copy Markdown
Contributor

It is coming from Python, as of 3.12; this will eventually turn into a SyntaxError. We could just route this specific warning to logs based on the invalid escape sequence verbiage.

This is indeed coming from Python 3.12, but something is trying to run or analyze the code, resulting in the warning being emitted. My guess is that it's the LSP, since that's the only thing that runs code when a user changes the contents of a document.

I suspect that the line of code that's triggering this warning would also trigger other warnings in the console. If we can find that line, we can catch and reroute all warnings to logs and avoid further work down the line.

@isabelizimm

Copy link
Copy Markdown
Contributor Author

Ahh-- searching around I do see that jedi-language-server is compiling on diagnostics; I'll add a context manager to silence SyntaxWarning just in that portion so you still get the warning when you run the cell 👍

https://github.com/pappasam/jedi-language-server/blob/9df0ab5c3481c4e637a8cc1dc3bb5e7afb11c960/jedi_language_server/jedi_utils.py#L302


# 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

@isabelizimm isabelizimm May 2, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes a bug where executed lines with SyntaxWarnings will return multiple times, since you would get the warning from 1) IPython's attempted compile and 2) actual runtime

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@seeM seeM left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Might be worth catching all warnings so we don't need to update this again if another leaks through

try:
diagnostic = jedi_utils.lsp_python_diagnostic(uri, doc.source)
with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than ignore SyntaxWarnings, can we redirect all warnings to logger.warning?


# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@isabelizimm isabelizimm merged commit 2e3827d into main May 6, 2024
@isabelizimm isabelizimm deleted the silence-syntaxwarning branch May 6, 2024 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants