-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
ABCs for MappingViews should declare __slots__ so subclasses aren't forced to have __dict__/__weakref__ #65620
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
Comments
Unlike the other collections ABCs, MappingView and its subclasses (Keys|Items|Values)View don't define __slots__, so users inheriting them to take advantage of the mix-in methods get a __dict__ and __weakref__, even if they try to avoid it by declaring their own __slots__. This is sub-optimal (I don't think any library class should leave __slots__ undefined, but it's particularly bad when they're intended to be used a base classes). I've attached a patch that defines __slots__ for all of them; MappingView explicitly declares its _mapping slot, and the rest declare no slots at all. Only negative I can think of is that if the user provides their own __init__, doesn't call the super().__init__, and uses a different name than _mapping for whatever data structure they actually use, then there will be a pointer reserved for _mapping that never gets set. That said, if they don't define _mapping, none of the mix-in methods work anyway, so they may as well not inherit directly, and instead just use *View.register to become a virtual subclass. |
Hmm... First patch isn't generating a review diff (I'm guessing because my VM's time sync went farkakte). Trying again. |
I also wrote a slightly more thorough patch that simplifies some of the method implementations (largely just replacing a bunch of for/if/return True/False else return False/True with any/all calls against a generator expression). The one behavior difference is that I dramatically simplified Sequence's __iter__; the original just kept indexing until it got IndexError, the replacement just runs len(self) times, on the theory that a self-mutating Sequence should write its own __iter__, and we shouldn't jump through hoops to accommodate unsupported behavior like mutating a Sequence during iteration. All tests pass under both patches. |
At first glance, this seems like a reasonable request. |
New changeset 2c6a231e2c1e by Raymond Hettinger in branch 'default': |
I've applied the __slots__ patch. Thank you for submitting it. Am leaving the rest of the ABCs code as-is. The current form may be a bit wordy but it is clean, fast, and easy to trace through a debugger. The expanded forms were chosen for a reason. |
Cool. Thanks for the feedback. I split it into two patches for a reason; I wasn't wedded to the simplification. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: