diff options
author | Yurii Karabas <1998uriyyo@gmail.com> | 2020-12-30 09:51:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-30 09:51:24 (GMT) |
commit | c56387f80c5aabf8100ceaffe365cc004ce0d7e0 (patch) | |
tree | c45546d1a311c87f58e9721a1bd232096ff07335 /Lib | |
parent | ba0e49a4648e727d1a16b3ce479499eb39f22311 (diff) | |
download | cpython-c56387f80c5aabf8100ceaffe365cc004ce0d7e0.zip cpython-c56387f80c5aabf8100ceaffe365cc004ce0d7e0.tar.gz cpython-c56387f80c5aabf8100ceaffe365cc004ce0d7e0.tar.bz2 |
bpo-27794: Add `name` attribute to `property` class (GH-23967)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_property.py | 50 | ||||
-rw-r--r-- | Lib/test/test_sys.py | 2 |
2 files changed, 51 insertions, 1 deletions
diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py index 172737a..7f3813f 100644 --- a/Lib/test/test_property.py +++ b/Lib/test/test_property.py @@ -204,6 +204,16 @@ class PropertyTests(unittest.TestCase): return 'Second' self.assertEqual(A.__doc__, 'Second') + def test_property_set_name_incorrect_args(self): + p = property() + + for i in (0, 1, 3): + with self.assertRaisesRegex( + TypeError, + fr'^__set_name__\(\) takes 2 positional arguments but {i} were given$' + ): + p.__set_name__(*([0] * i)) + # Issue 5890: subclasses of property do not preserve method __doc__ strings class PropertySub(property): @@ -299,6 +309,46 @@ class PropertySubclassTests(unittest.TestCase): self.assertEqual(Foo.spam.__doc__, "a new docstring") +class _PropertyUnreachableAttribute: + msg_format = None + obj = None + cls = None + + def _format_exc_msg(self, msg): + return self.msg_format.format(msg) + + @classmethod + def setUpClass(cls): + cls.obj = cls.cls() + + def test_get_property(self): + with self.assertRaisesRegex(AttributeError, self._format_exc_msg("unreadable attribute")): + self.obj.foo + + def test_set_property(self): + with self.assertRaisesRegex(AttributeError, self._format_exc_msg("can't set attribute")): + self.obj.foo = None + + def test_del_property(self): + with self.assertRaisesRegex(AttributeError, self._format_exc_msg("can't delete attribute")): + del self.obj.foo + + +class PropertyUnreachableAttributeWithName(_PropertyUnreachableAttribute, unittest.TestCase): + msg_format = "^{} 'foo'$" + + class cls: + foo = property() + + +class PropertyUnreachableAttributeNoName(_PropertyUnreachableAttribute, unittest.TestCase): + msg_format = "^{}$" + + class cls: + pass + + cls.foo = property() + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 3860656..3af5b11 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1329,7 +1329,7 @@ class SizeofTest(unittest.TestCase): def setx(self, value): self.__x = value def delx(self): del self.__x x = property(getx, setx, delx, "") - check(x, size('4Pi')) + check(x, size('5Pi')) # PyCapsule # XXX # rangeiterator |