From e03ea37a7bea48c46e6d96851f471db0f3c8e6e2 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 25 Sep 2013 07:14:41 -0700 Subject: Close #19030: improvements to inspect and Enum. inspect.getmembers and inspect.classify_class_attrs now search the metaclass mro for types.DynamicClassAttributes (what use to be called enum._RouteClassAttributeToGetattr); in part this means that these two functions no longer rely solely on dir(). Besides now returning more accurate information, these improvements also allow a more helpful help() on Enum classes. --- Lib/enum.py | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) (limited to 'Lib/enum.py') diff --git a/Lib/enum.py b/Lib/enum.py index 0a7afc45c0..45bfbb4e29 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1,36 +1,10 @@ import sys from collections import OrderedDict -from types import MappingProxyType +from types import MappingProxyType, DynamicClassAttribute __all__ = ['Enum', 'IntEnum', 'unique'] -class _RouteClassAttributeToGetattr: - """Route attribute access on a class to __getattr__. - - This is a descriptor, used to define attributes that act differently when - accessed through an instance and through a class. Instance access remains - normal, but access to an attribute through a class will be routed to the - class's __getattr__ method; this is done by raising AttributeError. - - """ - def __init__(self, fget=None): - self.fget = fget - if fget.__doc__ is not None: - self.__doc__ = fget.__doc__ - - def __get__(self, instance, ownerclass=None): - if instance is None: - raise AttributeError() - return self.fget(instance) - - def __set__(self, instance, value): - raise AttributeError("can't set attribute") - - def __delete__(self, instance): - raise AttributeError("can't delete attribute") - - def _is_descriptor(obj): """Returns True if obj is a descriptor, False otherwise.""" return ( @@ -504,12 +478,12 @@ class Enum(metaclass=EnumMeta): # members are not set directly on the enum class -- __getattr__ is # used to look them up. - @_RouteClassAttributeToGetattr + @DynamicClassAttribute def name(self): """The name of the Enum member.""" return self._name_ - @_RouteClassAttributeToGetattr + @DynamicClassAttribute def value(self): """The value of the Enum member.""" return self._value_ -- cgit v1.2.1