Skip to content

Commit dffff55

Browse files
committed
add ruff and bump python
1 parent fc7dd9d commit dffff55

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ jobs:
4545

4646
- name: Test flake8
4747
run: flake8 matplotlib_inline --ignore=E501,W504,W503
48+
49+
- name: Install ruff
50+
run: mamba install ruff
51+
52+
- name: Check code with ruff
53+
run: ruff check matplotlib_inline
54+
55+
- name: Check formatting with ruff
56+
run: ruff format matplotlib_inline --check

matplotlib_inline/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import backend_inline, config # noqa
2+
23
__version__ = "0.1.7" # noqa

matplotlib_inline/backend_inline.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ def new_figure_manager_given_figure(num, figure):
4343
# https://github.com/ipython/ipython/issues/1612
4444
# https://github.com/matplotlib/matplotlib/issues/835
4545

46-
if not hasattr(figure, 'show'):
46+
if not hasattr(figure, "show"):
4747
# Queue up `figure` for display
4848
figure.show = lambda *a: display(
49-
figure, metadata=_fetch_figure_metadata(figure))
49+
figure, metadata=_fetch_figure_metadata(figure)
50+
)
5051

5152
# If matplotlib was manually set to non-interactive mode, this function
5253
# should be a no-op (otherwise we'll generate duplicate plots, since a user
@@ -89,14 +90,14 @@ def show(close=None, block=None):
8990
for figure_manager in Gcf.get_all_fig_managers():
9091
display(
9192
figure_manager.canvas.figure,
92-
metadata=_fetch_figure_metadata(figure_manager.canvas.figure)
93+
metadata=_fetch_figure_metadata(figure_manager.canvas.figure),
9394
)
9495
finally:
9596
show._to_draw = []
9697
# only call close('all') if any to close
9798
# close triggers gc.collect, which can be slow
9899
if close and Gcf.get_all_fig_managers():
99-
matplotlib.pyplot.close('all')
100+
matplotlib.pyplot.close("all")
100101

101102

102103
# This flag will be reset by draw_if_interactive when called
@@ -176,8 +177,8 @@ def configure_inline_support(shell, backend):
176177
if cfg not in shell.configurables:
177178
shell.configurables.append(cfg)
178179

179-
if backend in ('inline', 'module://matplotlib_inline.backend_inline'):
180-
shell.events.register('post_execute', flush_figures)
180+
if backend in ("inline", "module://matplotlib_inline.backend_inline"):
181+
shell.events.register("post_execute", flush_figures)
181182

182183
# Save rcParams that will be overwrittern
183184
shell._saved_rcParams = {}
@@ -188,10 +189,10 @@ def configure_inline_support(shell, backend):
188189
new_backend_name = "inline"
189190
else:
190191
try:
191-
shell.events.unregister('post_execute', flush_figures)
192+
shell.events.unregister("post_execute", flush_figures)
192193
except ValueError:
193194
pass
194-
if hasattr(shell, '_saved_rcParams'):
195+
if hasattr(shell, "_saved_rcParams"):
195196
matplotlib.rcParams.update(shell._saved_rcParams)
196197
del shell._saved_rcParams
197198
new_backend_name = "other"
@@ -209,10 +210,12 @@ def configure_inline_support(shell, backend):
209210
def _enable_matplotlib_integration():
210211
"""Enable extra IPython matplotlib integration when we are loaded as the matplotlib backend."""
211212
from matplotlib import get_backend
213+
212214
ip = get_ipython()
213215
backend = get_backend()
214-
if ip and backend in ('inline', 'module://matplotlib_inline.backend_inline'):
216+
if ip and backend in ("inline", "module://matplotlib_inline.backend_inline"):
215217
from IPython.core.pylabtools import activate_matplotlib
218+
216219
try:
217220
activate_matplotlib(backend)
218221
configure_inline_support(ip, backend)
@@ -221,8 +224,9 @@ def _enable_matplotlib_integration():
221224
def configure_once(*args):
222225
activate_matplotlib(backend)
223226
configure_inline_support(ip, backend)
224-
ip.events.unregister('post_run_cell', configure_once)
225-
ip.events.register('post_run_cell', configure_once)
227+
ip.events.unregister("post_run_cell", configure_once)
228+
229+
ip.events.register("post_run_cell", configure_once)
226230

227231

228232
_enable_matplotlib_integration()
@@ -233,13 +237,17 @@ def _fetch_figure_metadata(fig):
233237
# determine if a background is needed for legibility
234238
if _is_transparent(fig.get_facecolor()):
235239
# the background is transparent
236-
ticksLight = _is_light([label.get_color()
237-
for axes in fig.axes
238-
for axis in (axes.xaxis, axes.yaxis)
239-
for label in axis.get_ticklabels()])
240+
ticksLight = _is_light(
241+
[
242+
label.get_color()
243+
for axes in fig.axes
244+
for axis in (axes.xaxis, axes.yaxis)
245+
for label in axis.get_ticklabels()
246+
]
247+
)
240248
if ticksLight.size and (ticksLight == ticksLight[0]).all():
241249
# there are one or more tick labels, all with the same lightness
242-
return {'needs_background': 'dark' if ticksLight[0] else 'light'}
250+
return {"needs_background": "dark" if ticksLight[0] else "light"}
243251

244252
return None
245253

@@ -249,13 +257,13 @@ def _is_light(color):
249257
opposed to dark). Based on ITU BT.601 luminance formula (see
250258
https://stackoverflow.com/a/596241)."""
251259
rgbaArr = colors.to_rgba_array(color)
252-
return rgbaArr[:, :3].dot((.299, .587, .114)) > .5
260+
return rgbaArr[:, :3].dot((0.299, 0.587, 0.114)) > 0.5
253261

254262

255263
def _is_transparent(color):
256264
"""Determine transparency from alpha."""
257265
rgba = colors.to_rgba(color)
258-
return rgba[3] < .5
266+
return rgba[3] < 0.5
259267

260268

261269
def set_matplotlib_formats(*formats, **kwargs):

matplotlib_inline/config.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
# Distributed under the terms of the BSD 3-Clause License.
88

99
from traitlets.config.configurable import SingletonConfigurable
10-
from traitlets import (
11-
Dict, Instance, Set, Bool, TraitError, Unicode
12-
)
10+
from traitlets import Dict, Instance, Set, Bool, TraitError, Unicode
1311

1412

1513
# Configurable for inline backend options
@@ -18,6 +16,7 @@ def pil_available():
1816
out = False
1917
try:
2018
from PIL import Image # noqa
19+
2120
out = True
2221
except ImportError:
2322
pass
@@ -44,37 +43,44 @@ class InlineBackend(InlineBackendConfig):
4443
the box, but third-party tools may use it to manage rc data. To change
4544
personal defaults for matplotlib, use matplotlib's configuration
4645
tools, or customize this class in your `ipython_config.py` file for
47-
IPython/Jupyter-specific usage.""").tag(config=True)
46+
IPython/Jupyter-specific usage.""",
47+
).tag(config=True)
4848

4949
figure_formats = Set(
50-
{'png'},
50+
{"png"},
5151
help="""A set of figure formats to enable: 'png',
52-
'retina', 'jpeg', 'svg', 'pdf'.""").tag(config=True)
52+
'retina', 'jpeg', 'svg', 'pdf'.""",
53+
).tag(config=True)
5354

5455
def _update_figure_formatters(self):
5556
if self.shell is not None:
5657
from IPython.core.pylabtools import select_figure_formats
57-
select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
58+
59+
select_figure_formats(
60+
self.shell, self.figure_formats, **self.print_figure_kwargs
61+
)
5862

5963
def _figure_formats_changed(self, name, old, new):
60-
if 'jpg' in new or 'jpeg' in new:
64+
if "jpg" in new or "jpeg" in new:
6165
if not pil_available():
6266
raise TraitError("Requires PIL/Pillow for JPG figures")
6367
self._update_figure_formatters()
6468

65-
figure_format = Unicode(help="""The figure format to enable (deprecated
66-
use `figure_formats` instead)""").tag(config=True)
69+
figure_format = Unicode(
70+
help="""The figure format to enable (deprecated
71+
use `figure_formats` instead)"""
72+
).tag(config=True)
6773

6874
def _figure_format_changed(self, name, old, new):
6975
if new:
7076
self.figure_formats = {new}
7177

7278
print_figure_kwargs = Dict(
73-
{'bbox_inches': 'tight'},
79+
{"bbox_inches": "tight"},
7480
help="""Extra kwargs to be passed to fig.canvas.print_figure.
7581
7682
Logical examples include: bbox_inches, quality (for jpeg figures), etc.
77-
"""
83+
""",
7884
).tag(config=True)
7985
_print_figure_kwargs_changed = _update_figure_formatters
8086

@@ -94,7 +100,9 @@ def _figure_format_changed(self, name, old, new):
94100
iterative editing of figures, and behaves most consistently with
95101
other matplotlib backends, but figure barriers between cells must
96102
be explicit.
97-
""").tag(config=True)
103+
""",
104+
).tag(config=True)
98105

99-
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
100-
allow_none=True)
106+
shell = Instance(
107+
"IPython.core.interactiveshell.InteractiveShellABC", allow_none=True
108+
)

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ classifiers = [
2020
"License :: OSI Approved :: BSD License",
2121
"Programming Language :: Python",
2222
"Programming Language :: Python :: 3",
23-
"Programming Language :: Python :: 3.8",
2423
"Programming Language :: Python :: 3.9",
2524
"Programming Language :: Python :: 3.10",
2625
"Programming Language :: Python :: 3.11",
@@ -37,7 +36,7 @@ keywords = [
3736
]
3837
license = {file = "LICENSE"}
3938
readme = "README.md"
40-
requires-python = ">=3.8"
39+
requires-python = ">=3.9"
4140

4241
[project.entry-points."matplotlib.backend"]
4342
inline = "matplotlib_inline.backend_inline"

0 commit comments

Comments
 (0)