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

Fix test failures with ipython 8.11 #35235

Closed
wants to merge 3 commits into from

Conversation

antonio-rojas
Copy link
Contributor

📚 Description

ipython 8.11 throws new warnings "UserWarning: Shell was already running a gui event loop for sage; switching to sage." when running some tests

📝 Checklist

  • I have made sure that the title is self-explanatory and the description concisely explains the PR.
  • I have linked an issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation accordingly.

⌛ Dependencies

@antonio-rojas antonio-rojas requested review from kiwifb and tornaria March 5, 2023 18:26
@github-actions
Copy link

github-actions bot commented Mar 5, 2023

Documentation preview for this PR is ready! 🎉
Built with commit: 3654f2e

@kiwifb
Copy link
Member

kiwifb commented Mar 5, 2023

Yes, I can see those.

Copy link
Member

@kiwifb kiwifb left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2023

Codecov Report

Patch coverage: 97.95% and project coverage change: +0.03 🎉

Comparison is base (e28bd1a) 88.58% compared to head (e81462a) 88.61%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #35235      +/-   ##
===========================================
+ Coverage    88.58%   88.61%   +0.03%     
===========================================
  Files         2141     2148       +7     
  Lines       397475   398654    +1179     
===========================================
+ Hits        352106   353287    +1181     
+ Misses       45369    45367       -2     
Impacted Files Coverage Δ
src/sage/schemes/elliptic_curves/ell_generic.py 93.38% <66.66%> (+0.15%) ⬆️
src/sage/interfaces/tachyon.py 87.93% <90.00%> (+0.43%) ⬆️
src/sage/schemes/elliptic_curves/gal_reps.py 82.23% <90.00%> (+0.04%) ⬆️
src/sage/quadratic_forms/quadratic_form.py 90.26% <95.65%> (+0.16%) ⬆️
src/sage/modular/quasimodform/element.py 99.20% <100.00%> (+0.06%) ⬆️
src/sage/repl/inputhook.py 68.96% <100.00%> (+1.10%) ⬆️
src/sage/rings/qqbar.py 95.30% <100.00%> (+<0.01%) ⬆️
src/sage/schemes/affine/affine_morphism.py 90.33% <100.00%> (ø)
src/sage/schemes/elliptic_curves/BSD.py 43.75% <100.00%> (+0.21%) ⬆️
src/sage/schemes/elliptic_curves/cardinality.py 87.54% <100.00%> (+0.14%) ⬆️
... and 41 more

... and 60 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tornaria
Copy link
Contributor

tornaria commented Mar 6, 2023

Hmm... are we sure the warning only happens on these doctests?

Wouldn't it be better to either fix or ignore the warning? Someone put the warning there for some reason. Even if the warning is irrelevant, did you make sure it won't show up to a normal user?

@tornaria
Copy link
Contributor

tornaria commented Mar 6, 2023

Indeed:

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.8, Release Date: 2023-02-11                     │
│ Using Python 3.11.2. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: attach("f.py")
sage: f()
42
[... edit the file ...]
sage: f()
43
### reloading attached file f.py modified at 13:24:00 ###
/usr/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py:917: UserWarning: Shell was already running a gui event loop for sage; switching to sage.
  warn(
sage: 

I think we should figure out if the warning means something has to be changed, or if the warning can be safely ignored in general, and keep the doctest as is so it makes sure the warning doesn't reappear.

@tornaria
Copy link
Contributor

tornaria commented Mar 6, 2023

As of fb2a1f9 the inputhook is not installed once at initialization but each time a file is attached (including when attached files are reloaded). The warning complains about that.

The following seems to fix it: the install() function checks whether the inputhook is already ours. I'm not sure this is the proper fix, maybe @vbraun can comment / review since he wrote this code.

The call to get_test_shell() in the doctest is necessary, otherwise get_ipython() returns None and nothing is tested!

I still don't know how to replace the branch in a PR like we used to do in trac. Opening a new PR seems too much noise.

--- a/src/sage/repl/inputhook.py
+++ b/src/sage/repl/inputhook.py
@@ -49,13 +49,23 @@ def install():
 
     EXAMPLES::
 
+    Make sure ipython is running so we really test this function
+
+        sage: from sage.repl.interpreter import get_test_shell
+        sage: get_test_shell()
+        <sage.repl.interpreter.SageTestShell object at ...>
+
+    Test the function twice, to make sure it is idempotent (see :trac:`35235`)
+
         sage: from sage.repl.inputhook import install
         sage: install()
+        sage: install()
     """
     ip = get_ipython()
     if not ip:
         return   # Not running in ipython, e.g. doctests
-    ip.enable_gui('sage')
+    if ip._inputhook != sage_inputhook:
+        ip.enable_gui('sage')
 
 
 def uninstall():

@kiwifb
Copy link
Member

kiwifb commented Mar 6, 2023

I certainly do like the approach. Is it compatible with older ipython or does it require us to move to ipython 8.11? I would not have an issue with that but I would be surprised if it was needed.

@tornaria
Copy link
Contributor

tornaria commented Mar 6, 2023

I certainly do like the approach. Is it compatible with older ipython or does it require us to move to ipython 8.11? I would not have an issue with that but I would be surprised if it was needed.

I didn't test but I don't think it is, indeed, all checks have passed here using older ipython.

All we do here is to skip changing the inputhook if it is already set to our own. In this case ip.enable_gui('sage') would be a noop except for the warning.

@tornaria
Copy link
Contributor

tornaria commented Mar 8, 2023

I pushed the branch with my fix to tornaria:ipython-8.11. The commit is tornaria@502cb00, in case it helps.

I don't know if it's possible to replace antonio-rojas:ipython-8.11 by tornaria:ipython-8.11 in this PR, maybe @arojas can just grab my commit.

@antonio-rojas
Copy link
Contributor Author

Done

@antonio-rojas
Copy link
Contributor Author

@tornaria tox -e rst is failing with ./sage/repl/inputhook.py:51:1: RST218 Literal block expected; none found.. I have no idea what that means or how to fix it, please take a look
`

@kiwifb
Copy link
Member

kiwifb commented Mar 13, 2023

Looking at other files I believe you need something like'

EXAMPLES: <-notice only one ":"

some text:: <- introduce the double "::" here

    code
    code
    code

some more text:: <- again here

    more code

@tornaria
Copy link
Contributor

Sorry, I usually don't build docs... I'll redo my commit.

It turns out running tox -e rst takes about 2 minutes and I guess it checks the whole source tree. Is there a way to run this test for a single file? For development it would be useful.

Can someone link to documentation explaining the doctest formats?

@tornaria
Copy link
Contributor

I have updated my branch tornaria:ipython-8.11. The new commit is tornaria@23471e2.

Note that I rebased on top of beta4 and redid the commit -- you won't be able to merge, just resetting your branch to mine seems the easier way (also, I find it very noisy that we keep a commit + revert the commit).

I'm still wondering: maybe the workflow should be that we close this PR and I open a separate one. It's very unfortunate that there is so much friction to work cooperatively on a PR. @mkoeppe any suggestions?

@antonio-rojas antonio-rojas deleted the ipython-8.11 branch March 23, 2023 17:30
@antonio-rojas
Copy link
Contributor Author

Let's just open a new pull request with your branch

vbraun pushed a commit that referenced this pull request Apr 6, 2023
    
<!-- ^^^^^
Please provide a concise, informative and self-explanatory title.
Don't put issue numbers in there, do this in the PR body below.
For example, instead of "Fixes #1234" use "Introduce new method to
calculate 1+1"
-->
### 📚 Description

ipython 8.11 throws new warnings "UserWarning: Shell was already running
a gui event loop for sage; switching to sage." when running some tests.

This is because sage indeed tries to install the same gui event loop for
ipython again and again. The fix is checking if the ipython input hook
is already set to the sage input hook, in that case we don't install the
gui event loop again.

In the test, we run `install()` twice so we can verify that running it
multiple times won't cause a warning anymore.

<!-- Describe your changes here in detail -->
<!-- Why is this change required? What problem does it solve? -->
<!-- If it resolves an open issue, please link to the issue here. For
example "Closes #1337" -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] I have made sure that the title is self-explanatory and the
description concisely explains the PR.
- [x] I have linked an issue or discussion.
- [x] I have created tests covering the changes.

### ⌛ Dependencies
<!-- List all open pull requests that this PR logically depends on -->
<!--
- #xyz: short description why this is a dependency
- #abc: ...
-->

### Replaces: #35235
Since gh doesn't allow to change the branch on a PR.
    
URL: #35337
Reported by: Gonzalo Tornaría
Reviewer(s): François Bissey
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.

5 participants