Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upbpo-38293: Allow shallow and deep copying of property objects #16438
Conversation
Good catch @GudniNatan i am not copy expert but I test it and run successfully. I don't know if will need necessary a NEWS entry, IMO yes, but I would like hear some |
Thanks @GudniNatan, and welcome to CPython. This looks good! I think this should have a NEWS entry. It's a new feature (and there's precedent for it when Maybe just something like: Add :func:`copy.copy` and :func:`copy.deepcopy` support to :func:`property` objects. Just out of curiosity, what was your use case for wanting to copy these? |
This comment has been minimized.
This comment has been minimized.
I was experimenting with using property objects to validate data in dataclasses. Here is some code that should help illustrate:
The MyClass dataclass has an attribute my_value, and the idea is that if you set it to be anything other than a string, it will throw a ValueError. We were also using a library (dataclasses-jsonschema) that has some similar functionality to ClassSchema, in that it stores the default values of our dataclasses. It will then try to convert those to dictionaries with the dataclasses asdict function. However, asdict will try to give you a dictionary that is independent of the instance you feed it. To do that it uses the deepcopy, and then you get the So basically calling asdict() on certain dataclass instances results in this weird error. |
I have a nagging doubt. You can create a property from any callable, e.g. @GudniNatan For your use case, is deep-copyability required? |
This comment has been minimized.
This comment has been minimized.
@gvanrossum following that argument, |
This comment has been minimized.
This comment has been minimized.
@arigo That's fair. Tuples are special, because they are meant to be robust data structures, and a tuple containing a list is a well-known (and used) corner case. I'll approve. |
This comment has been minimized.
This comment has been minimized.
miss-islington
commented
Jan 12, 2020
Thanks @GudniNatan for the PR |
This comment has been minimized.
This comment has been minimized.
miss-islington
commented
Jan 12, 2020
Sorry @GudniNatan, I had trouble checking out the |
…GH-16438) Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293 (cherry picked from commit 9f3fc6c) Co-authored-by: Guðni Natan Gunnarsson <1493259+GudniNatan@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
bedevere-bot
commented
Jan 12, 2020
GH-17968 is a backport of this pull request to the 3.7 branch. |
This comment has been minimized.
This comment has been minimized.
miss-islington
commented
Jan 12, 2020
Thanks @GudniNatan for the PR |
This comment has been minimized.
This comment has been minimized.
bedevere-bot
commented
Jan 12, 2020
GH-17969 is a backport of this pull request to the 3.8 branch. |
…GH-16438) Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293 (cherry picked from commit 9f3fc6c) Co-authored-by: Guðni Natan Gunnarsson <1493259+GudniNatan@users.noreply.github.com>
Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293 (cherry picked from commit 9f3fc6c) Co-authored-by: Guðni Natan Gunnarsson <1493259+GudniNatan@users.noreply.github.com>
bpo-38293: Allow shallow and deep copying of property objects (pythonGH-16438)
Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293 (cherry picked from commit 9f3fc6c) Co-authored-by: Guðni Natan Gunnarsson <1493259+GudniNatan@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
Congrats on your first CPython contribution @GudniNatan! Looking forward to seeing more from you in the future. |
…GH-16438) Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293
…GH-16438) Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293
…GH-16438) Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293
GudniNatan commentedSep 27, 2019
•
edited by miss-islington
Copying property objects results in a TypeError. Steps to reproduce:
This affects both shallow and deep copying.
My idea for a fix is to add property objects to the list of "atomic" objects in the copy module.
These already include types like functions and type objects.
I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :)
https://bugs.python.org/issue38293
Automerge-Triggered-By: @csabella