Skip to content

Commit

Permalink
Merge pull request #22484 from jitseniesen/string-array
Browse files Browse the repository at this point in the history
PR: Change default format in array editor to ''
  • Loading branch information
ccordoba12 authored Sep 11, 2024
2 parents a89ee75 + ed46ef7 commit 2d2e0a9
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 2d2e0a9

Please sign in to comment.