Skip to content
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

Behavior of immutable attributes #237

Merged

Conversation

CleitonDeLima
Copy link
Contributor

When using get_inlines and get_inlines_names, a new copy of the object is returned, avoiding changing the class attribute.

@CleitonDeLima CleitonDeLima force-pushed the fix/inlines-attr-imutable branch from 6c788c1 to 5bb2378 Compare July 23, 2021 19:48
@codecov-commenter
Copy link

codecov-commenter commented Jul 23, 2021

Codecov Report

Merging #237 (7d11b65) into master (415ef28) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #237      +/-   ##
==========================================
+ Coverage   88.77%   88.79%   +0.02%     
==========================================
  Files           6        6              
  Lines         490      491       +1     
  Branches       54       54              
==========================================
+ Hits          435      436       +1     
  Misses         40       40              
  Partials       15       15              
Impacted Files Coverage Δ
extra_views/advanced.py 96.33% <100.00%> (+0.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 415ef28...7d11b65. Read the comment docs.

Copy link
Collaborator

@sdolemelipone sdolemelipone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks like a good idea. @jonashaag are you good to merge?

@jonashaag
Copy link
Collaborator

Is there something similar or identical in Django? Is copying a common approach in the Django codebase?

@sdolemelipone
Copy link
Collaborator

sdolemelipone commented Aug 11, 2021

When mutable class level attributes are returned by instance methods they are generally copied, e.g. see FormMixin.get_initial() here: https://github.com/django/django/blob/main/django/views/generic/edit.py

This commit differs from Django in that it is using deepcopy() rather than a shallow .copy() or [:].

@jonashaag
Copy link
Collaborator

Thank you! Shouldn’t we use copy as well? We don’t want to make copies of the classes, only return a fresh dict.

@sdolemelipone
Copy link
Collaborator

Yes, I think you're right! I like how asking the right question gets the right answer :-)

@CleitonDeLima would you re-write this to use [:] instead of deepcopy? Or can you suggest why deepcopy would be preferred over a shallow copy?

@CleitonDeLima
Copy link
Contributor Author

CleitonDeLima commented Aug 12, 2021

Yes, I think you're right! I like how asking the right question gets the right answer :-)

@CleitonDeLima would you re-write this to use [:] instead of deepcopy? Or can you suggest why deepcopy would be preferred over a shallow copy?

Hi @sdolemelipone ,
It really could just be the copy, I can change that.
I didn't use [:] because tuples can be used.

a = (1, 2, 3)
id(a) == id(a[:])  # is True

@sdolemelipone
Copy link
Collaborator

Thanks! LGTM.

Just as a side discussion, as tuples are immutable, does the [:] matter? I think we are only protecting against mutable lists being modified via the instance.

@CleitonDeLima
Copy link
Contributor Author

CleitonDeLima commented Aug 12, 2021

Thanks! LGTM.

Just as a side discussion, as tuples are immutable, does the [:] matter? I think we are only protecting against mutable lists being modified via the instance.

Of course! I'm getting in my way on basic concepts, treat the problem with [:] is enough. 😅
Thanks!

@sdolemelipone
Copy link
Collaborator

Even better! 😄 Thanks, I hadn't thought about what happens when you call [:] on a tuple until now.

@jonashaag
Copy link
Collaborator

I'd have preferred the explicit copy way personally but this is fine. Thanks everyone!

@jonashaag jonashaag merged commit 96b8c68 into AndrewIngram:master Aug 12, 2021
@sdolemelipone
Copy link
Collaborator

I'd have preferred the explicit copy way personally but this is fine. Thanks everyone!

Interesting! Why, because explicit is better than implicit? For me [:] appeared more beautiful/elegant.
Also, I didn't know that:

from copy import copy
a = (1, 2, 3)
id(a) == id(copy(a))  # is True

@CleitonDeLima CleitonDeLima deleted the fix/inlines-attr-imutable branch August 12, 2021 21:13
@jonashaag
Copy link
Collaborator

Yes, and because it’s done that way in the Django codebase. Needlessly doing things differently in one place than in the other increases chances of creating bugs, adds to the mental load of readers, etc.

@Azd325 Azd325 mentioned this pull request Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants