copy() method on Style object raises TypeError

Issue #444 resolved
John O'Brien created an issue

Starting in 2.2.0, the copy method only works on style components, like Font objects, but not on Style objects.

>>> from openpyxl.styles import Style
>>> s0 = Style()
>>> s1 = s0.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/obrienjw/venv/py27-openpyxl220/lib/python2.7/site-packages/openpyxl-2.2.0-py2.7.egg/openpyxl/styles/hashable.py", line 28, in copy
    return self.__class__(**current)
  File "/Users/obrienjw/venv/py27-openpyxl220/lib/python2.7/site-packages/openpyxl-2.2.0-py2.7.egg/openpyxl/styles/__init__.py", line 42, in __init__
    self._font = font
  File "/Users/obrienjw/venv/py27-openpyxl220/lib/python2.7/site-packages/openpyxl-2.2.0-py2.7.egg/openpyxl/descriptors/base.py", line 35, in __set__
    raise TypeError('expected ' + str(self.expected_type))
TypeError: expected <class 'openpyxl.styles.fonts.Font'>

Previously, in 2.1.5:

>>> from openpyxl.styles import Style
>>> s0 = Style()
>>> s1 = s0.copy()
>>>

Comments (5)

  1. CharlieC

    This has now been resolved. However, it's worth noting that from 2.2 Style objects are essentially deprecated because you can work with the relevant formatting objects such as font, number format, etc. directly.

  2. Jean-Baptiste Quenot

    I'm sorry but the deprecated copy() method you re-introduced when resolving this issue does not have the same signature as in version 2.1.4. When upgrading to 2.2.2 I get this error:

    TypeError: copy() got an unexpected keyword argument 'font'
    

    I used to call the copy method like this:

    newStyle = myStyle.copy(font=Font(bold=True))
    

    Is it possible to have a smooth upgrade path and restore the missing keyword arguments to the copy method?

  3. CharlieC

    @Jean-Baptiste Quenot Very unlikely unless you make a pull request.

    Style() objects for individual cells are essentially an artefact of the way things are stored by Excel. To be useful they are horribly inefficient.

  4. Log in to comment