FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141510: Fix frozendict.fromkeys() for subclasses by vstinner · Pull Request #144947 · python/cpython · GitHub

/ cpython Public

gh-141510: Fix frozendict.fromkeys() for subclasses - #144947

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:frozendict_fromkeys
Closed

gh-141510: Fix frozendict.fromkeys() for subclasses#144947
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:frozendict_fromkeys

Conversation

vstinner commented Feb 18, 2026
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Don't call the constructor of the subclass, but frozendict constructor instead.

Don't call the constructor of the subclass, but frozendict
constructor instead.

corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?

Copy link
Copy Markdown
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:

frozendict({'x': None, 'y': None})

That's bad: the immutable frozendict is mutated!

corona10 commented Feb 18, 2026
edited
Loading

Copy link
Copy Markdown
Member

How about copy whole object at

else if (PyFrozenDict_CheckExact(d)) {
not to mutate original frozendict simply?

It follows the original intention of immutable data structures: copy instead of mutating.

vstinner marked this pull request as draft February 18, 2026 13:03

Copy link
Copy Markdown
Member Author

How about copy whole object

Ok, good idea: I wrote #144952 to copy the frozendict.

Copy link
Copy Markdown
Member Author

I close this PR in favor of #144952 fix.

vstinner closed this Feb 18, 2026
vstinner deleted the frozendict_fromkeys branch February 18, 2026 15:37
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL