diff --git a/spyder/plugins/variableexplorer/widgets/arrayeditor.py b/spyder/plugins/variableexplorer/widgets/arrayeditor.py index bd06d2d262a..0effb87fdba 100644 --- a/spyder/plugins/variableexplorer/widgets/arrayeditor.py +++ b/spyder/plugins/variableexplorer/widgets/arrayeditor.py @@ -71,7 +71,7 @@ class ArrayEditorWidgets: ToolbarStretcher = 'toolbar_stretcher' -# Note: string and unicode data types will be formatted with 's' (see below) +# Note: string and unicode data types will be formatted with '' (see below) SUPPORTED_FORMATS = { 'single': '.6g', 'double': '.6g', @@ -657,7 +657,10 @@ def __init__(self, parent, data, readonly=False): self.old_data_shape = self.data.shape self.data.shape = (1, 1) - format_spec = SUPPORTED_FORMATS.get(data.dtype.name, 's') + # Use '' as default format specifier, because 's' does not produce + # a `str` for arrays with strings, see spyder-ide/spyder#22466 + format_spec = SUPPORTED_FORMATS.get(data.dtype.name, '') + self.model = ArrayModel(self.data, format_spec=format_spec, readonly=readonly, parent=self) self.view = ArrayView(self, self.model, data.dtype, data.shape) diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_arrayeditor.py b/spyder/plugins/variableexplorer/widgets/tests/test_arrayeditor.py index b482575267e..f7fd7773fef 100644 --- a/spyder/plugins/variableexplorer/widgets/tests/test_arrayeditor.py +++ b/spyder/plugins/variableexplorer/widgets/tests/test_arrayeditor.py @@ -115,6 +115,23 @@ def test_type_errors(setup_arrayeditor, qtbot): assert_array_equal(contents, np.ones(10)) +@pytest.mark.parametrize( + 'data', + [np.array([['a', 'b'], ['c', 'd']])] +) +def test_string_array_data_is_str(setup_arrayeditor): + """ + Verify that the displayed data of an array of strings is of type `str`. + + Regression test for spyder-ide/spyder#22466. + """ + dlg = setup_arrayeditor + idx = dlg.arraywidget.model.index(0, 0) + data = dlg.arraywidget.model.data(idx) + assert data == 'a' + assert type(data) is str + + @pytest.mark.skipif(not sys.platform.startswith('linux'), reason="Only works on Linux") @pytest.mark.parametrize(