def test_with_binding_descriptor(self): cls = type(mocks.ClassWithDescriptor(mocks.Binding( mocks.Descriptor()))) result = get_descriptor(cls, attrname) self.assertIs(result, cls.__dict__[attrname])
def name_of(descriptor, owner): """ Given a descriptor and a class that the descriptor is stored on, returns the name of the attribute the descriptor is stored under. Also works if the given class is a subclass of the class that *actually* has the descriptor attribute :param descriptor: descriptor the name is being looked up for :param owner: class that "owns" the descriptor :return: the name the descriptor is stored under on *owner* """ return _first(attr for attr in dir(owner) if (get_descriptor(owner, attr) is descriptor))
def name_of(descriptor, owner): """ Given a descriptor and a class that the descriptor is stored on, returns the name of the attribute the descriptor is stored under. Also works if the given class is a subclass of the class that *actually* has the descriptor attribute :param descriptor: descriptor the name is being looked up for :param owner: class that "owns" the descriptor :return: the name the descriptor is stored under on *owner* """ name = _first(attr for attr in dir(owner) if (get_descriptor(owner, attr) is descriptor)) if name is None: raise RuntimeError( str.format("The descriptor, '{}', does not exist on type, '{}'", descriptor, owner.__qualname__)) return name
def test_with_superclass(self): result = get_descriptor(SubClass, 'attr') self.assertIs(result, Class.__dict__['attr'])
def test_with_binding_descriptor(self): cls = type(mocks.ClassWithDescriptor(mocks.Binding(mocks.Descriptor()))) result = get_descriptor(cls, attrname) self.assertIs(result, cls.__dict__[attrname])