This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Deepcopying property objects results in unexpected TypeError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: GudniNatan, eamanu, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-09-27 16:16 by GudniNatan, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16438 merged GudniNatan, 2019-09-27 19:13
PR 17968 merged miss-islington, 2020-01-12 17:42
PR 17969 merged miss-islington, 2020-01-12 17:45
Messages (9)
msg353375 - (view) Author: Guðni Nathan (GudniNatan) * Date: 2019-09-27 16:16
Currently, attempting to deepcopy a property object will result in an unexpected TypeError:

>>> import copy
>>> obj = property()
>>> new_obj = copy.deepcopy(obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1264.0_x64__qbz5n2kfra8p0\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)
TypeError: can't pickle property objects


What I believe is happening here is that since property objects are not treated by the copy module as atomic, they are passed off to be pickled and so our error is raised.
This can be fixed in a similar manner to how it works for type objects, function objects and more.
Adding a single line of code to Lib/copy.py after line 208:

    d[property] = _deepcopy_atomic

Means that property objects will be treated as atomic, and therefore returned as-is.
msg353376 - (view) Author: Guðni Nathan (GudniNatan) * Date: 2019-09-27 16:19
A small change:

The fix should go to Lib/copy.py:198, not line 208.
msg353377 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-27 16:48
But the property object is not atomic. It's attribute __doc__ is writeable.
msg353383 - (view) Author: Guðni Nathan (GudniNatan) * Date: 2019-09-27 17:27
Function objects are considered "atomic" here and I believe you can also write to their __doc__ (among other attributes).
msg353388 - (view) Author: Guðni Nathan (GudniNatan) * Date: 2019-09-27 18:40
This bug appears to also affect shallow copies and can be reproduced with the following code:

>>> import copy
>>> obj = property()
>>> copy.copy(obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1264.0_x64__qbz5n2kfra8p0\lib\copy.py", line 96, in copy
    rv = reductor(4)
TypeError: can't pickle property objects
msg353428 - (view) Author: Emmanuel Arias (eamanu) * Date: 2019-09-27 23:55
I can confirm this behavior also on python 3.6 3.8 3.9
msg359857 - (view) Author: miss-islington (miss-islington) Date: 2020-01-12 17:42
New changeset 9f3fc6c5b4993f2b362263b494f84793a21aa073 by Miss Islington (bot) (Guðni Natan Gunnarsson) in branch 'master':
bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
https://github.com/python/cpython/commit/9f3fc6c5b4993f2b362263b494f84793a21aa073
msg359860 - (view) Author: miss-islington (miss-islington) Date: 2020-01-12 18:00
New changeset 4be97260351f214d3c8b8477682323bb52ee2af3 by Miss Islington (bot) in branch '3.7':
bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
https://github.com/python/cpython/commit/4be97260351f214d3c8b8477682323bb52ee2af3
msg359861 - (view) Author: miss-islington (miss-islington) Date: 2020-01-12 18:04
New changeset 3043ec7d6aed402218404c25179e734166c7fbe0 by Miss Islington (bot) in branch '3.8':
bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
https://github.com/python/cpython/commit/3043ec7d6aed402218404c25179e734166c7fbe0
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82474
2020-01-12 18:04:21miss-islingtonsetmessages: + msg359861
2020-01-12 18:00:30miss-islingtonsetmessages: + msg359860
2020-01-12 17:45:35miss-islingtonsetpull_requests: + pull_request17377
2020-01-12 17:43:50cheryl.sabellasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 3.6
2020-01-12 17:42:12miss-islingtonsetpull_requests: + pull_request17376
2020-01-12 17:42:04miss-islingtonsetnosy: + miss-islington
messages: + msg359857
2019-09-27 23:55:53eamanusetmessages: + msg353428
versions: + Python 3.6, Python 3.8, Python 3.9
2019-09-27 23:38:45eamanusetnosy: + eamanu
2019-09-27 19:13:27GudniNatansetkeywords: + patch
stage: patch review
pull_requests: + pull_request16016
2019-09-27 18:40:01GudniNatansetmessages: + msg353388
2019-09-27 17:27:23GudniNatansetmessages: + msg353383
2019-09-27 16:48:27serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg353377
2019-09-27 16:19:13GudniNatansetmessages: + msg353376
2019-09-27 16:16:43GudniNatancreate