Skip to content

[Bug]: ValueError upon deepcopy of a Figure object #21554

@tovrstra

Description

@tovrstra

Bug summary

A deepcopy of a Figure object results in ValueError: 'Spines' object does not contain a '__deepcopy__' spine.

This issue surfaced in a bug report of schemdraw: https://bitbucket.org/cdelker/schemdraw/issues/56/copydeepcopy-drawing-fails Nevertheless, the current issue is unrelated to schemdraw. I'm just mentioning this to illustrate a relevant use case leading to this problem.

Code for reproduction

import matplotlib.pyplot as plt
import copy
fig, ax = plt.subplots()
copy.deepcopy(fig)

Disclaimer: this is taken from a message of cdelker on https://bitbucket.org/cdelker/schemdraw/issues/56/copydeepcopy-drawing-fails

Actual outcome

Traceback (most recent call last):
  File "/usr/lib64/python3.9/site-packages/matplotlib/spines.py", line 551, in __getattr__
    return self._dict[name]
KeyError: '__deepcopy__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib64/python3.9/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib64/python3.9/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 205, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 210, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "/usr/lib64/python3.9/copy.py", line 210, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "/usr/lib64/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib64/python3.9/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 151, in deepcopy
    copier = getattr(x, "__deepcopy__", None)
  File "/usr/lib64/python3.9/site-packages/matplotlib/spines.py", line 553, in __getattr__
    raise ValueError(
ValueError: 'Spines' object does not contain a '__deepcopy__' spine

Expected outcome

Either a deepcopy of the figure or a meaningful error message explaining that this operation is not supported.

Operating system

Fedora Linux

Matplotlib Version

3.4.3

Matplotlib Backend

Qt5Agg

Python version

3.9.7

Jupyter version

6.1.6, (not installed)

Other libraries

None

Installation

Linux package manager (Debian/Fedora/etc.)

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions