question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with subclasses now no longer using isinstance

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jmcnamaracommented, Aug 15, 2018

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?

1reaction
jmcnamaracommented, Aug 15, 2018

Test and fixed on 29b411b

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are the differences between type() and isinstance()?
isinstance () returns a boolean - true or false - based on whether the object is of given type. isinstance is usually more...
Read more >
`isinstance(instance, SubClass)` loses generic type instead of ...
Describe the bug When performing an isinstance check on object with generic type, the type information of variable type is lost.
Read more >
Difference between type() and isinstance() in Python
To conclude, isinstance() considers inheritance, but checking the type() of an object using equality ( == ) statement does not. A subclass ......
Read more >
type() vs. isinstance() - Sebastian Witowski
Python is a dynamically typed language. A variable, initially created as a string, can be later reassigned to an integer or a float....
Read more >
Issue 32683: isinstance is calling ob.__getattribute__ as ...
All my code was doing was to check that a certain object was not of "callable" type using isinstance. Whilst I believe no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found