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 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:

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/", line 43, in export_set
        dset_sheet(dataset, ws, freeze_panes=freeze_panes)
      File "tablib/formats/", line 147, in dset_sheet
        cell.value = unicode(col)
      File "openpyxl/cell/", line 252, in value
      File "openpyxl/cell/", 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