[New-bugs-announce] [issue21421] ABCs for MappingViews should declare __slots__ so subclasses aren't forced to have __dict__/__weakref__

Josh Rosenberg report at bugs.python.org
Sat May 3 02:02:41 CEST 2014

New submission from Josh Rosenberg:

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.

files: slots_for_mappingview_abcs.patch
keywords: patch
messages: 217809
nosy: josh.rosenberg
priority: normal
severity: normal
status: open
title: ABCs for MappingViews should declare __slots__ so subclasses aren't forced to have __dict__/__weakref__
Added file: http://bugs.python.org/file35141/slots_for_mappingview_abcs.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list