From 4d2c9e32619254c1ea170e897f2c3220ab3419e8 Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Fri, 28 Jun 2024 09:54:05 +0100 Subject: [PATCH 1/2] Don't force matplotlib backend names to be lowercase --- IPython/core/magics/pylab.py | 2 +- IPython/core/tests/test_pylabtools.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/IPython/core/magics/pylab.py b/IPython/core/magics/pylab.py index 265f860063a..423498d404c 100644 --- a/IPython/core/magics/pylab.py +++ b/IPython/core/magics/pylab.py @@ -100,7 +100,7 @@ def matplotlib(self, line=''): % _list_matplotlib_backends_and_gui_loops() ) else: - gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui) + gui, backend = self.shell.enable_matplotlib(args.gui) self._show_matplotlib_backend(args.gui, backend) @skip_doctest diff --git a/IPython/core/tests/test_pylabtools.py b/IPython/core/tests/test_pylabtools.py index 6bddb348077..e9d98ff4646 100644 --- a/IPython/core/tests/test_pylabtools.py +++ b/IPython/core/tests/test_pylabtools.py @@ -350,3 +350,27 @@ def test_backend_entry_point(): def test_backend_unknown(): with pytest.raises(RuntimeError if pt._matplotlib_manages_backends() else KeyError): pt.find_gui_and_backend("name-does-not-exist") + + +@dec.skipif(not pt._matplotlib_manages_backends()) +def test_backend_module_name_case_sensitive(): + # Matplotlib backend names are case insensitive unless explicitly specified using + # "module://some_module.some_name" syntax which are case sensitive for mpl >= 3.9.1 + all_lowercase = "module://matplotlib_inline.backend_inline" + some_uppercase = "module://matplotlib_inline.Backend_inline" + ip = get_ipython() + mpl3_9_1 = matplotlib.__version_info__ >= (3, 9, 1) + + ip.enable_matplotlib(all_lowercase) + if mpl3_9_1: + with pytest.raises(RuntimeError): + ip.enable_matplotlib(some_uppercase) + else: + ip.enable_matplotlib(some_uppercase) + + ip.run_line_magic("matplotlib", all_lowercase) + if mpl3_9_1: + with pytest.raises(RuntimeError): + ip.run_line_magic("matplotlib", some_uppercase) + else: + ip.run_line_magic("matplotlib", some_uppercase) From 95b627553645dada0ba9e338b0da7005ecfe3fd4 Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Fri, 28 Jun 2024 11:02:26 +0100 Subject: [PATCH 2/2] Isolate tests so state doesn't leak to other tests --- IPython/core/tests/test_pylabtools.py | 47 +++++++++++++-------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/IPython/core/tests/test_pylabtools.py b/IPython/core/tests/test_pylabtools.py index e9d98ff4646..31d3dbe21f2 100644 --- a/IPython/core/tests/test_pylabtools.py +++ b/IPython/core/tests/test_pylabtools.py @@ -258,6 +258,29 @@ def test_qt_gtk(self): assert gui == "qt" assert s.pylab_gui_select == "qt" + @dec.skipif(not pt._matplotlib_manages_backends()) + def test_backend_module_name_case_sensitive(self): + # Matplotlib backend names are case insensitive unless explicitly specified using + # "module://some_module.some_name" syntax which are case sensitive for mpl >= 3.9.1 + all_lowercase = "module://matplotlib_inline.backend_inline" + some_uppercase = "module://matplotlib_inline.Backend_inline" + mpl3_9_1 = matplotlib.__version_info__ >= (3, 9, 1) + + s = self.Shell() + s.enable_matplotlib(all_lowercase) + if mpl3_9_1: + with pytest.raises(RuntimeError): + s.enable_matplotlib(some_uppercase) + else: + s.enable_matplotlib(some_uppercase) + + s.run_line_magic("matplotlib", all_lowercase) + if mpl3_9_1: + with pytest.raises(RuntimeError): + s.run_line_magic("matplotlib", some_uppercase) + else: + s.run_line_magic("matplotlib", some_uppercase) + def test_no_gui_backends(): for k in ['agg', 'svg', 'pdf', 'ps']: @@ -350,27 +373,3 @@ def test_backend_entry_point(): def test_backend_unknown(): with pytest.raises(RuntimeError if pt._matplotlib_manages_backends() else KeyError): pt.find_gui_and_backend("name-does-not-exist") - - -@dec.skipif(not pt._matplotlib_manages_backends()) -def test_backend_module_name_case_sensitive(): - # Matplotlib backend names are case insensitive unless explicitly specified using - # "module://some_module.some_name" syntax which are case sensitive for mpl >= 3.9.1 - all_lowercase = "module://matplotlib_inline.backend_inline" - some_uppercase = "module://matplotlib_inline.Backend_inline" - ip = get_ipython() - mpl3_9_1 = matplotlib.__version_info__ >= (3, 9, 1) - - ip.enable_matplotlib(all_lowercase) - if mpl3_9_1: - with pytest.raises(RuntimeError): - ip.enable_matplotlib(some_uppercase) - else: - ip.enable_matplotlib(some_uppercase) - - ip.run_line_magic("matplotlib", all_lowercase) - if mpl3_9_1: - with pytest.raises(RuntimeError): - ip.run_line_magic("matplotlib", some_uppercase) - else: - ip.run_line_magic("matplotlib", some_uppercase)