Strange tab completion oddity with enums?

Peter Otten __peter__ at web.de
Tue Oct 8 03:06:45 EDT 2019


Piet van Oostrum wrote:

> Chris Angelico <rosuav at gmail.com> writes:
> 
>> I'm not sure what's going on here, and it's probably not actually
>> enum-specific, but that's where I saw it.
>>
>> If you create a plain class and have an attribute with an annotation,
>> you can see that:
>>
>>>>> class Foo:
>> ...     spam: "ham" = 1
>> ...
>>>>> Foo.__a
>> Foo.__abstractmethods__  Foo.__annotations__
>>>>> Foo.__annotations__
>> {'spam': 'ham'}
> 
> Also strange:
> 
> It shows Foo.__abstractmethods__ but there is no such attribute.
> What's going on?
> 
>>>> Foo.__abstractmethods__
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: __abstractmethods__

An AttributeError doesn't generally doesn't mean that the attribute doesn't 
exist. Consider:

>>> class Foo:
...     @property
...     def bar(self): raise AttributeError
... 
>>> foo = Foo()
>>> "bar" in dir(foo)
True
>>> foo.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in bar
AttributeError

So __abstractmethods__ might be a property of Foo's metaclass (type). 
Let's see:

>>> type.__dict__["__abstractmethods__"]
<attribute '__abstractmethods__' of 'type' objects>
>>> type.__dict__["__abstractmethods__"].__get__(Foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __abstractmethods__






More information about the Python-list mailing list