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

Class attributes shadow __slots__ #1

Open
tseaver opened this issue Jan 19, 2015 · 2 comments
Open

Class attributes shadow __slots__ #1

tseaver opened this issue Jan 19, 2015 · 2 comments
Labels

Comments

@tseaver
Copy link
Member

tseaver commented Jan 19, 2015

In https://bugs.launchpad.net/zope.proxy/+bug/186631, @ctheune reported:

Define a subclass from SpecificationDecoratorBase with slots:

class MyProxy(SpecificationDecoratorBase):
    __slots__ = ('foo',)
    foo = None

Now, when using an instance of MyProxy the following will happen:

>> f = MyProxy([])
>> print f.foo
None
>> f.foo = 'bar'
>> print f.foo
None

When not assigning foo as a class attribute, it works. IMHO this isn't exactly intended to work this way, but the tests don't specify anything here.

@tseaver tseaver added the bug label Jan 19, 2015
@tseaver
Copy link
Member Author

tseaver commented Jan 19, 2015

I don't reproduce this exactly, but the behavior is still odd:

>>> class MyProxy(SpecificationDecoratorBase):
...     __slots__ = ('foo',)
...     foo = None
... 
>>> f = MyProxy([])
>>> print f.foo
None
>>> f.foo = 'bar'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'foo'

@jamadden
Copy link
Member

I'm not sure this is a bug. You get the same basic thing without involving a proxy:

>>> class MyObject(object):
...     __slots__ = ('foo',)
...     foo = None
...
>>> f = MyObject()
>>> print f.foo
None
>>> f.foo = 'bar'
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'MyObject' object attribute 'foo' is read-only

The presence of the foo class attribute suppresses the creation of the normal slot descriptor for foo that would be done by the metaclass:

>>> MyObject.__dict__
{'__module__': '__main__', '__slots__': ('foo',), 'foo': None, '__doc__': None}
# Compare to an absent class attribute
>>> class MyOtherObject(object):
...     __slots__ = ('foo',)
...
>>> MyOtherObject.__dict__
{'foo': <member 'foo' of 'MyOtherObject' objects>, '__slots__': ('foo',), '__module__': '__main__', '__doc__': None}

But __slots__ has already customized the instance layout, and since there's no descriptor, and no __dict__, foo is unavailable on the instance. So this behaviour seems reasonable to me, albeit obscure. It matches the wording in language spec on slots:

__slots__ are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by __slots__; otherwise, the class attribute would overwrite the descriptor assignment.

Now, where it gets weird in the proxy case is if the underlying object can accept a foo attribute. Then, instead of getting set on the proxy object, it gets passed through to the underlying object (and it's still not accessible through the proxy):

>>> class Any(object): pass
...
>>> any = Any()
>>> f = MyObject(any)
>>> f.foo
>>> f.foo = 'bar'
>>> f.foo
>>> any.foo
'bar'

I'm not sure if that's a genuine bug, or if we're in corner-cases of the language specification because we're trying to use __slots__ and class attributes at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants