Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] log positron_ipykernel warnings, do not show to user #2776

Merged
merged 9 commits into from
Apr 19, 2024

Conversation

isabelizimm
Copy link
Contributor

@isabelizimm isabelizimm commented Apr 16, 2024

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.

related to #2615 #2446 (sort of #2075)

Approach

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

Using a custom handler for warnings. It checks if the warning is coming from python_files/positron/positron_ipykernel and then routes it to the logs. If the error is coming from the console, do not show temp filename. You can still override this filter, eg, if you would like to raise DeprecationWarnings in the console, etc.

Video of what warnings should look like ⏬

warnings.mov

todo

  • add tests
  • windows paths?

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.

import warnings

warnings.warn("this is a warning")

should not see temp path before warning (something like var/folders/.../47329.py)

@isabelizimm
Copy link
Contributor Author

isabelizimm commented Apr 18, 2024

(merge conflict resulting in strange errors 😩, all good now?)

isabelizimm and others added 3 commits April 18, 2024 15:58
Signed-off-by: Isabel Zimmerman <54685329+isabelizimm@users.noreply.github.com>
@isabelizimm isabelizimm marked this pull request as ready for review April 18, 2024 20:52
@isabelizimm isabelizimm requested a review from seeM April 18, 2024 20:52
Copy link
Contributor

@seeM seeM left a comment

Choose a reason for hiding this comment

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

LGTM! I left a couple suggestions that aren't critical, but I think may improve this a little more


# monkey patching warning.showwarning is recommended by the official documentation
# https://docs.python.org/3/library/warnings.html#warnings.showwarning
def _showwarning(message, category, filename, lineno, file=None, line=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, thanks for including this comment, I was surprised they recommend this!

Could it be more robust if we call down to the original functions with modified arguments instead of calling _showwarnmsg_impl directly?

It looks like they keep references at warnings._showwarning_orig and warnings._formatwarning_orig (source). It's slightly risky that they change this in Python 3.13 or later, but we should see a test failure and be able to fix it then so I'm not too concerned.

# https://docs.python.org/3/library/warnings.html#warnings.showwarning
def _showwarning(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("python_files", "positron", "positron_ipykernel")
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we get this path dynamically so it's robust to name changes? I think we could use Path(__file__).parent

# vendored as-is from warnings
def _formatwarning(message, category, filename, lineno, line=None):
# --- Start Positron ---
if filename == "POSITRON_CONSOLE":
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than omitting the filename and line number for warnings from console cells, what if we displayed the cell number and line number instead? Something like:

<positron-console-cell-4>:2: UserWarning: message

We may be able to get the cell number from kernel.execution_count. But if it turns out to be too difficult, then maybe even <positron-console> is fine?

Then we also wouldn't need to override formatwarning, we could directly pass the filename through in showwarning.


For reference, here's what other shells do:

  • Python: <stdin>:2: UserWarning: message
  • IPython: <ipython-input-1-b68dd3b18f8f>:2: UserWarning message
  • ipykernel: /var/folders/_r/pm17dpfn7lx5mf3wtd96kw3m0000gn/T/ipykernel_49873/4243969252.py:2: UserWarning: message

(Out of curiosity, I dug into why IPython and ipykernel modify the filenames in different ways. It seems that ipykernel made the change when they implementing debugging (ipython/ipykernel#597) since they needed something that could be calculated given the cell source in both the frontend and the kernel. IIUC that file isn't actually ever created.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that's a helpful reference for the other shells! I'll see if I can grab the cell/line, looks nice and maybe will feel more familiar for users who are expecting some sort of guidance of where the warning is coming from 👍

@@ -308,3 +319,27 @@ def test_question_mark_help(shell: PositronShell, mock_help_service: Mock) -> No
mock_help_service.show_help.assert_called_once_with(
"positron_ipykernel.utils.positron_ipykernel_usage"
)


def test_console_warning(shell: PositronShell, warning_kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice tests!

isabelizimm and others added 2 commits April 19, 2024 09:35
…ernel/positron_ipkernel.py

Co-authored-by: Wasim Lorgat <mwlorgat@gmail.com>
Signed-off-by: Isabel Zimmerman <54685329+isabelizimm@users.noreply.github.com>
@isabelizimm isabelizimm merged commit f4f85dd into main Apr 19, 2024
24 checks passed
@isabelizimm isabelizimm deleted the fix/console-warnings-to-logs branch April 19, 2024 15:03
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.

2 participants