Issue with subclasses now no longer using isinstance
See original GitHub issueI am using django so some of my strings are subclasses of str
, more specifically they are SafeText
instances. This means that the changes to speed up the write
method no longer do isinstance
checks and instead check the content passed though are actual str
types. Unfortunately this makes the library less pythonic (“if it quacks like a duck…”). Further, apart from not writing this as a string to the workbook it also uncovers an issue where unicode
is no longer a keyword in python 3 and so I get the below stack trace.
I am using Python version 3.6 and XlsxWriter 1.0.6
Here is some code that demonstrates the problem:
In [3]: class MyStr(str):
...: pass
...:
...:
In [4]: import xlsxwriter
In [5]: workbook = xlsxwriter.Workbook('hello.xlsx')
In [6]: worksheet = workbook.add_worksheet()
In [7]: worksheet.write('A1', MyStr('Hello world'))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-ba253f798a36> in <module>()
----> 1 worksheet.write('A1', MyStr('Hello world'))
~/.local/share/virtualenvs/AgricolaWeb-bDFxwp9T/lib/python3.7/site-packages/xlsxwriter/worksheet.py in cell_wrapper(self, *args, **kwargs)
64 args = new_args + args[1:]
65
---> 66 return method(self, *args, **kwargs)
67
68 return cell_wrapper
~/.local/share/virtualenvs/AgricolaWeb-bDFxwp9T/lib/python3.7/site-packages/xlsxwriter/worksheet.py in write(self, row, col, *args)
402
403 """
--> 404 return self._write(row, col, *args)
405
406 # Undecorated version of write().
~/.local/share/virtualenvs/AgricolaWeb-bDFxwp9T/lib/python3.7/site-packages/xlsxwriter/worksheet.py in _write(self, row, col, *args)
435 return self._write_datetime(row, col, *args)
436
--> 437 if token_type is unicode:
438 try:
439 return self._write_token_as_string(str(token), row, col, *args)
NameError: name 'unicode' is not defined
In [8]: isinstance(MyStr('Hello world'), str)
Out[8]: True
In the meantime I will pin to 1.0.5 as that is simpler than going though and figuring out if all the places I pass strings to get written are subclasses of str
.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top GitHub Comments
Thanks for the report. Definitely a bug. I’ll revert the
unicode
part of the change and release an update in the next 12 hours.@percious Any comments?
Test and fixed on 29b411b