Cannot convert django SafeText to Excel

Issue #1230 wontfix
Cristi Vîjdea
created an issue

Django implements str subclasses for some of its functionality. When trying to export these with openpyxl, one has to take special care to convert them into plain string, for example via "" + s. This is inconvenient and ugly.

I believe this to be a regression, since openpyxl 2.5 accepted them just fine.

The problem stems from the use of is and in for type checks in cell.py instead of issubclass of isinstance. I was going to open a PR to fix that before I noticed that they were deliberate changes introduced in:

https://bitbucket.org/openpyxl/openpyxl/commits/f6ab07d60361dc5bc255c589553b97920c3d9b3e

https://bitbucket.org/openpyxl/openpyxl/commits/88621f7efef96853d48b2cbb50faa2b2d713a936

I believe the aforementioned changes are steps in the wrong direction and needlessly restrict the input types, going against most OOP and duck typing principles.

Maybe related to #1229?

Comments (2)

  1. Cristi Vîjdea reporter

    I'm sorry but that's a really backwards conclusion to draw from this. Client code is passing instances of a str subclass in a perfectly valid way.
    No part of the openpyxl api or documentation implies that only objects of the exact str class will be accepted.

    The error message is also really confusing:

    ValueError: Cannot convert '' to Excel
      [...]
      File "tablib/formats/_xlsx.py", line 43, in export_set
        dset_sheet(dataset, ws, freeze_panes=freeze_panes)
      File "tablib/formats/_xlsx.py", line 147, in dset_sheet
        cell.value = unicode(col)
      File "openpyxl/cell/cell.py", line 252, in value
        self._bind_value(value)
      File "openpyxl/cell/cell.py", line 218, in _bind_value
        raise ValueError("Cannot convert {0!r} to Excel".format(value))
    

    Here '' is actually SafeText('') which passes isinstace(str), but not t in STRING_TYPES. issubclass(t, STRING_TYPES) would be acceptable.
    This is the same issue as #1181

  2. Log in to comment