diff options
author | Aaron Hall, MBA <aaronchall@yahoo.com> | 2018-05-20 23:46:42 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2018-05-20 23:46:42 (GMT) |
commit | 4054b172ab59054715a2aaf4979bd84ac23e3ada (patch) | |
tree | e21f94afd65ee1cd88e011649fd6147f03567a12 /Lib/test/test_inspect.py | |
parent | aef639f62677f8a342af24e9c19f0503b0d1e36e (diff) | |
download | cpython-4054b172ab59054715a2aaf4979bd84ac23e3ada.zip cpython-4054b172ab59054715a2aaf4979bd84ac23e3ada.tar.gz cpython-4054b172ab59054715a2aaf4979bd84ac23e3ada.tar.bz2 |
bpo-26103: Fix inspect.isdatadescriptor() and a data descriptor definition. (GH-1959)
Look for '__set__' or '__delete__'.
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r-- | Lib/test/test_inspect.py | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 3481a57..ee227a6 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -1134,6 +1134,61 @@ class TestClassesAndFunctions(unittest.TestCase): attrs = [a[0] for a in inspect.getmembers(C)] self.assertNotIn('missing', attrs) +class TestIsDataDescriptor(unittest.TestCase): + + def test_custom_descriptors(self): + class NonDataDescriptor: + def __get__(self, value, type=None): pass + class DataDescriptor0: + def __set__(self, name, value): pass + class DataDescriptor1: + def __delete__(self, name): pass + class DataDescriptor2: + __set__ = None + self.assertFalse(inspect.isdatadescriptor(NonDataDescriptor()), + 'class with only __get__ not a data descriptor') + self.assertTrue(inspect.isdatadescriptor(DataDescriptor0()), + 'class with __set__ is a data descriptor') + self.assertTrue(inspect.isdatadescriptor(DataDescriptor1()), + 'class with __delete__ is a data descriptor') + self.assertTrue(inspect.isdatadescriptor(DataDescriptor2()), + 'class with __set__ = None is a data descriptor') + + def test_slot(self): + class Slotted: + __slots__ = 'foo', + self.assertTrue(inspect.isdatadescriptor(Slotted.foo), + 'a slot is a data descriptor') + + def test_property(self): + class Propertied: + @property + def a_property(self): + pass + self.assertTrue(inspect.isdatadescriptor(Propertied.a_property), + 'a property is a data descriptor') + + def test_functions(self): + class Test(object): + def instance_method(self): pass + @classmethod + def class_method(cls): pass + @staticmethod + def static_method(): pass + def function(): + pass + a_lambda = lambda: None + self.assertFalse(inspect.isdatadescriptor(Test().instance_method), + 'a instance method is not a data descriptor') + self.assertFalse(inspect.isdatadescriptor(Test().class_method), + 'a class method is not a data descriptor') + self.assertFalse(inspect.isdatadescriptor(Test().static_method), + 'a static method is not a data descriptor') + self.assertFalse(inspect.isdatadescriptor(function), + 'a function is not a data descriptor') + self.assertFalse(inspect.isdatadescriptor(a_lambda), + 'a lambda is not a data descriptor') + _global_ref = object() class TestGetClosureVars(unittest.TestCase): @@ -3792,7 +3847,7 @@ def test_main(): TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, TestBoundArguments, TestSignaturePrivateHelpers, - TestSignatureDefinitions, + TestSignatureDefinitions, TestIsDataDescriptor, TestGetClosureVars, TestUnwrap, TestMain, TestReload, TestGetCoroutineState ) |