-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-38293: Allow shallow and deep copying of property objects #16438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
core dev opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 copy
/deepcopy
support was added for other objects in the past). Creating one is super quick!
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?
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a nagging doubt. You can create a property from any callable, e.g. property(lambda self: None)
. The argument is not type-checked, so you can also create a property from a non-callable, e.g. property([1, 2, 3])
. When we deep-copy such a property, shouldn't we deep-copy the list (i.e. the fget
attribute)? After all, when we deep-copy a tuple containing a list, the list gets copied.
@GudniNatan For your use case, is deep-copyability required?
@gvanrossum following that argument, |
@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. |
Thanks @GudniNatan for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7, 3.8. |
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>
GH-17968 is a backport of this pull request to the 3.7 branch. |
Thanks @GudniNatan for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8. |
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>
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
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