Skip to content

Commit

Permalink
Change default format in array editor to ''
Browse files Browse the repository at this point in the history
Before, the default format in the array editor for non-numeric arrays
was set to 's'. This meant that ArrayModel.data() returned a value of
type numpy.str_ for string arrays instead of a value of type str. The
consequence was that the contents of the array were not shown. Changing
the default format to '' fixes this.

Also add a regression test.
  • Loading branch information
jitseniesen committed Sep 11, 2024
1 parent a89ee75 commit ed46ef7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions spyder/plugins/variableexplorer/widgets/arrayeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions spyder/plugins/variableexplorer/widgets/tests/test_arrayeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ed46ef7

Please sign in to comment.