-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
DEP: bump minimal supported IPython version to 7.32, declare ipykernel and ipywidgets as optional deps, cleanup #16354
Merged
+26
−48
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,11 @@ | |
_CAN_RESIZE_TERMINAL = False | ||
|
||
from astropy import conf | ||
from astropy.utils.compat.optional_deps import ( | ||
HAS_IPYKERNEL, | ||
HAS_IPYTHON, | ||
HAS_IPYWIDGETS, | ||
) | ||
|
||
from .decorators import classproperty, deprecated | ||
from .misc import isiterable | ||
|
@@ -47,44 +52,27 @@ | |
class _IPython: | ||
"""Singleton class given access to IPython streams, etc.""" | ||
|
||
@classproperty | ||
def get_ipython(cls): | ||
try: | ||
from IPython import get_ipython | ||
except ImportError: | ||
pass | ||
return get_ipython | ||
|
||
@classproperty | ||
def OutStream(cls): | ||
if not hasattr(cls, "_OutStream"): | ||
cls._OutStream = None | ||
try: | ||
cls.get_ipython() | ||
except NameError: | ||
return None | ||
|
||
try: | ||
if HAS_IPYKERNEL: | ||
from ipykernel.iostream import OutStream | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
except ImportError: | ||
try: | ||
from IPython.zmq.iostream import OutStream | ||
except ImportError: | ||
return None | ||
|
||
cls._OutStream = OutStream | ||
cls._OutStream = OutStream | ||
else: | ||
cls._OutStream = None | ||
|
||
return cls._OutStream | ||
|
||
@classproperty | ||
def ipyio(cls): | ||
if not hasattr(cls, "_ipyio"): | ||
try: | ||
if HAS_IPYTHON: | ||
from IPython.utils import io | ||
except ImportError: | ||
cls._ipyio = None | ||
else: | ||
|
||
cls._ipyio = io | ||
else: | ||
cls._ipyio = None | ||
return cls._ipyio | ||
|
||
|
||
|
@@ -108,26 +96,7 @@ | |
if _IPython.OutStream is None or (not isinstance(file, _IPython.OutStream)): | ||
return False | ||
|
||
# File is an IPython OutStream. Check whether: | ||
# - File name is 'stdout'; or | ||
# - File wraps a Console | ||
if getattr(file, "name", None) == "stdout": | ||
return True | ||
|
||
if hasattr(file, "stream"): | ||
# FIXME: pyreadline has no had new release since 2015, drop it when | ||
# IPython minversion is 5.x. | ||
# On Windows, in IPython 2 the standard I/O streams will wrap | ||
# pyreadline.Console objects if pyreadline is available; this should | ||
# be considered a TTY. | ||
try: | ||
from pyreadline.console import Console as PyreadlineConsole | ||
except ImportError: | ||
return False | ||
|
||
return isinstance(file.stream, PyreadlineConsole) | ||
|
||
return False | ||
return getattr(file, "name", None) == "stdout" | ||
|
||
|
||
@deprecated("6.1", alternative="shutil.get_terminal_size") | ||
|
@@ -586,9 +555,12 @@ | |
""" | ||
# Create and display an empty progress bar widget, | ||
# if none exists. | ||
|
||
if not hasattr(self, "_widget"): | ||
# Import only if an IPython widget, i.e., widget in iPython NB | ||
_IPython.get_ipython() | ||
if not HAS_IPYWIDGETS: | ||
raise ModuleNotFoundError("ipywidgets is not installed") | ||
|
||
from ipywidgets import widgets | ||
|
||
self._widget = widgets.FloatProgress() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what this NameError was for but looks like handling it has been dropped in this PR. Can you please clarify? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
get_ipython
method was oddly written in such a way that its return variable was only bound if IPython was installed, meaning it was effectively relying on a NameError being raised otherwise. Since I also rewrote the method in a more conventional manner, this handling isn't needed any moreThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, looking at code in
main
, the original design looks like it never intended to throw exception if IPython is not installed, but this PR does. Can we refactor in a way that does not throw exception? Thanks for your patience!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused
if you mean the
get_ipython
class method, it seems to me that it was actually designed to throw an exception since that's how it's been used throughexcept NameError
(albeit a quirky exception to catch in your own code).I don't understand, where else would an exception be raised in my version ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the
NameError
onmain
makes no sense to me. When I try to emulate the implementation, I getUnboundLocalError
. But that is neither here nor there...The point I was trying to make is that
get_ipython
onmain
defers the exception handling toOutStream
. But in this PR, you explicitly throwsModuleNotFoundError
from withinget_ipython
if IPython is not installed. I think that is a subtle behavior change?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually it was easy enough that I just went for it: #16369
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I figured that this
_IPython.get_ipython
method was not actually needed, so I refactored it. To make it easier for you to follow after I rebased, I've kept this one change as a separate commit (that should eventually get squashed).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, does look much cleaner now. But I still don't know what is the point of this whole
_IPython
class now that I dig into the code. I can't seem to see it being used anywhere and it is "hidden".Anyways, should we get #16369 in first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nvm, found it. Just used internally here for
isatty
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these PRs are actually completely independent as it turns out; they can be merged in any order