gh-141510: Fix frozendict.fromkeys() for subclasses#144947
Closed
vstinner wants to merge 1 commit intopython:mainfrom
Closed
gh-141510: Fix frozendict.fromkeys() for subclasses#144947vstinner wants to merge 1 commit intopython:mainfrom
vstinner wants to merge 1 commit intopython:mainfrom
Conversation
Don't call the constructor of the subclass, but frozendict constructor instead.
corona10
reviewed
Feb 18, 2026
Member
corona10
left a comment
There was a problem hiding this comment.
I think we should discuss this before taking any action here.
Do we really need to handle cases where users shoot themselves in the foot? It doesn’t cause a crash at the moment, right?
Member
Author
|
Currently, frozendict.fromkeys() allows modifying an immutable frozendict using a special subclass. Example: class SpecialDict(frozendict):
def __new__(self):
return frozendict(x=None)
print(SpecialDict.fromkeys("y"))Current output: That's bad: the immutable frozendict is mutated! |
Member
|
How about copy whole object at Line 3319 in dd64e42 It follows the original intention of immutable data structures: copy instead of mutating. |
Member
Author
Ok, good idea: I wrote #144952 to copy the frozendict. |
Member
Author
|
I close this PR in favor of #144952 fix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Don't call the constructor of the subclass, but frozendict constructor instead.