| 1 |
# Test case for property |
| 2 |
# more tests are in test_descr |
| 3 |
|
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
from test import support |
| 7 |
|
| 8 |
class PropertyBase(Exception): |
| 9 |
pass |
| 10 |
|
| 11 |
class PropertyGet(PropertyBase): |
| 12 |
pass |
| 13 |
|
| 14 |
class PropertySet(PropertyBase): |
| 15 |
pass |
| 16 |
|
| 17 |
class PropertyDel(PropertyBase): |
| 18 |
pass |
| 19 |
|
| 20 |
class BaseClass(object): |
| 21 |
def __init__(self): |
| 22 |
self._spam = 5 |
| 23 |
|
| 24 |
@property |
| 25 |
def spam(self): |
| 26 |
"""BaseClass.getter""" |
| 27 |
return self._spam |
| 28 |
|
| 29 |
@spam.setter |
| 30 |
def spam(self, value): |
| 31 |
self._spam = value |
| 32 |
|
| 33 |
@spam.deleter |
| 34 |
def spam(self): |
| 35 |
del self._spam |
| 36 |
|
| 37 |
class SubClass(BaseClass): |
| 38 |
|
| 39 |
@BaseClass.spam.getter |
| 40 |
def spam(self): |
| 41 |
"""SubClass.getter""" |
| 42 |
raise PropertyGet(self._spam) |
| 43 |
|
| 44 |
@spam.setter |
| 45 |
def spam(self, value): |
| 46 |
raise PropertySet(self._spam) |
| 47 |
|
| 48 |
@spam.deleter |
| 49 |
def spam(self): |
| 50 |
raise PropertyDel(self._spam) |
| 51 |
|
| 52 |
class PropertyDocBase(object): |
| 53 |
_spam = 1 |
| 54 |
def _get_spam(self): |
| 55 |
return self._spam |
| 56 |
spam = property(_get_spam, doc="spam spam spam") |
| 57 |
|
| 58 |
class PropertyDocSub(PropertyDocBase): |
| 59 |
@PropertyDocBase.spam.getter |
| 60 |
def spam(self): |
| 61 |
"""The decorator does not use this doc string""" |
| 62 |
return self._spam |
| 63 |
|
| 64 |
class PropertySubNewGetter(BaseClass): |
| 65 |
@BaseClass.spam.getter |
| 66 |
def spam(self): |
| 67 |
"""new docstring""" |
| 68 |
return 5 |
| 69 |
|
| 70 |
class PropertyNewGetter(object): |
| 71 |
@property |
| 72 |
def spam(self): |
| 73 |
"""original docstring""" |
| 74 |
return 1 |
| 75 |
@spam.getter |
| 76 |
def spam(self): |
| 77 |
"""new docstring""" |
| 78 |
return 8 |
| 79 |
|
| 80 |
class PropertyTests(unittest.TestCase): |
| 81 |
def test_property_decorator_baseclass(self): |
| 82 |
# see #1620 |
| 83 |
base = BaseClass() |
| 84 |
self.assertEqual(base.spam, 5) |
| 85 |
self.assertEqual(base._spam, 5) |
| 86 |
base.spam = 10 |
| 87 |
self.assertEqual(base.spam, 10) |
| 88 |
self.assertEqual(base._spam, 10) |
| 89 |
delattr(base, "spam") |
| 90 |
self.assertNotHasAttr(base, "spam") |
| 91 |
self.assertNotHasAttr(base, "_spam") |
| 92 |
base.spam = 20 |
| 93 |
self.assertEqual(base.spam, 20) |
| 94 |
self.assertEqual(base._spam, 20) |
| 95 |
|
| 96 |
def test_property_decorator_subclass(self): |
| 97 |
# see #1620 |
| 98 |
sub = SubClass() |
| 99 |
self.assertRaises(PropertyGet, getattr, sub, "spam") |
| 100 |
self.assertRaises(PropertySet, setattr, sub, "spam", None) |
| 101 |
self.assertRaises(PropertyDel, delattr, sub, "spam") |
| 102 |
|
| 103 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 104 |
"Docstrings are omitted with -O2 and above") |
| 105 |
def test_property_decorator_subclass_doc(self): |
| 106 |
sub = SubClass() |
| 107 |
self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter") |
| 108 |
|
| 109 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 110 |
"Docstrings are omitted with -O2 and above") |
| 111 |
def test_property_decorator_baseclass_doc(self): |
| 112 |
base = BaseClass() |
| 113 |
self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter") |
| 114 |
|
| 115 |
def test_property_decorator_doc(self): |
| 116 |
base = PropertyDocBase() |
| 117 |
sub = PropertyDocSub() |
| 118 |
self.assertEqual(base.__class__.spam.__doc__, "spam spam spam") |
| 119 |
self.assertEqual(sub.__class__.spam.__doc__, "spam spam spam") |
| 120 |
|
| 121 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 122 |
"Docstrings are omitted with -O2 and above") |
| 123 |
def test_property_getter_doc_override(self): |
| 124 |
newgettersub = PropertySubNewGetter() |
| 125 |
self.assertEqual(newgettersub.spam, 5) |
| 126 |
self.assertEqual(newgettersub.__class__.spam.__doc__, "new docstring") |
| 127 |
newgetter = PropertyNewGetter() |
| 128 |
self.assertEqual(newgetter.spam, 8) |
| 129 |
self.assertEqual(newgetter.__class__.spam.__doc__, "new docstring") |
| 130 |
|
| 131 |
def test_property___isabstractmethod__descriptor(self): |
| 132 |
for val in (True, False, [], [1], '', '1'): |
| 133 |
class C(object): |
| 134 |
def foo(self): |
| 135 |
pass |
| 136 |
foo.__isabstractmethod__ = val |
| 137 |
foo = property(foo) |
| 138 |
self.assertIs(C.foo.__isabstractmethod__, bool(val)) |
| 139 |
|
| 140 |
# check that the property's __isabstractmethod__ descriptor does the |
| 141 |
# right thing when presented with a value that fails truth testing: |
| 142 |
class NotBool(object): |
| 143 |
def __bool__(self): |
| 144 |
raise ValueError() |
| 145 |
__len__ = __bool__ |
| 146 |
with self.assertRaises(ValueError): |
| 147 |
class C(object): |
| 148 |
def foo(self): |
| 149 |
pass |
| 150 |
foo.__isabstractmethod__ = NotBool() |
| 151 |
foo = property(foo) |
| 152 |
C.foo.__isabstractmethod__ |
| 153 |
|
| 154 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 155 |
"Docstrings are omitted with -O2 and above") |
| 156 |
def test_property_builtin_doc_writable(self): |
| 157 |
p = property(doc='basic') |
| 158 |
self.assertEqual(p.__doc__, 'basic') |
| 159 |
p.__doc__ = 'extended' |
| 160 |
self.assertEqual(p.__doc__, 'extended') |
| 161 |
|
| 162 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 163 |
"Docstrings are omitted with -O2 and above") |
| 164 |
def test_property_decorator_doc_writable(self): |
| 165 |
class PropertyWritableDoc(object): |
| 166 |
|
| 167 |
@property |
| 168 |
def spam(self): |
| 169 |
"""Eggs""" |
| 170 |
return "eggs" |
| 171 |
|
| 172 |
sub = PropertyWritableDoc() |
| 173 |
self.assertEqual(sub.__class__.spam.__doc__, 'Eggs') |
| 174 |
sub.__class__.spam.__doc__ = 'Spam' |
| 175 |
self.assertEqual(sub.__class__.spam.__doc__, 'Spam') |
| 176 |
|
| 177 |
@support.refcount_test |
| 178 |
def test_refleaks_in___init__(self): |
| 179 |
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') |
| 180 |
fake_prop = property('fget', 'fset', 'fdel', 'doc') |
| 181 |
refs_before = gettotalrefcount() |
| 182 |
for i in range(100): |
| 183 |
fake_prop.__init__('fget', 'fset', 'fdel', 'doc') |
| 184 |
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) |
| 185 |
|
| 186 |
@support.refcount_test |
| 187 |
def test_gh_115618(self): |
| 188 |
# Py_XDECREF() was improperly called for None argument |
| 189 |
# in property methods. |
| 190 |
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') |
| 191 |
prop = property() |
| 192 |
refs_before = gettotalrefcount() |
| 193 |
for i in range(100): |
| 194 |
prop = prop.getter(None) |
| 195 |
self.assertIsNone(prop.fget) |
| 196 |
for i in range(100): |
| 197 |
prop = prop.setter(None) |
| 198 |
self.assertIsNone(prop.fset) |
| 199 |
for i in range(100): |
| 200 |
prop = prop.deleter(None) |
| 201 |
self.assertIsNone(prop.fdel) |
| 202 |
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) |
| 203 |
|
| 204 |
def test_property_name(self): |
| 205 |
def getter(self): |
| 206 |
return 42 |
| 207 |
|
| 208 |
def setter(self, value): |
| 209 |
pass |
| 210 |
|
| 211 |
class A: |
| 212 |
@property |
| 213 |
def foo(self): |
| 214 |
return 1 |
| 215 |
|
| 216 |
@foo.setter |
| 217 |
def oof(self, value): |
| 218 |
pass |
| 219 |
|
| 220 |
bar = property(getter) |
| 221 |
baz = property(None, setter) |
| 222 |
|
| 223 |
self.assertEqual(A.foo.__name__, 'foo') |
| 224 |
self.assertEqual(A.oof.__name__, 'oof') |
| 225 |
self.assertEqual(A.bar.__name__, 'bar') |
| 226 |
self.assertEqual(A.baz.__name__, 'baz') |
| 227 |
|
| 228 |
A.quux = property(getter) |
| 229 |
self.assertEqual(A.quux.__name__, 'getter') |
| 230 |
A.quux.__name__ = 'myquux' |
| 231 |
self.assertEqual(A.quux.__name__, 'myquux') |
| 232 |
self.assertEqual(A.bar.__name__, 'bar') # not affected |
| 233 |
A.quux.__name__ = None |
| 234 |
self.assertIsNone(A.quux.__name__) |
| 235 |
|
| 236 |
with self.assertRaisesRegex( |
| 237 |
AttributeError, "'property' object has no attribute '__name__'" |
| 238 |
): |
| 239 |
property(None, setter).__name__ |
| 240 |
|
| 241 |
with self.assertRaisesRegex( |
| 242 |
AttributeError, "'property' object has no attribute '__name__'" |
| 243 |
): |
| 244 |
property(1).__name__ |
| 245 |
|
| 246 |
class Err: |
| 247 |
def __getattr__(self, attr): |
| 248 |
raise RuntimeError('fail') |
| 249 |
|
| 250 |
p = property(Err()) |
| 251 |
with self.assertRaisesRegex(RuntimeError, 'fail'): |
| 252 |
p.__name__ |
| 253 |
|
| 254 |
p.__name__ = 'not_fail' |
| 255 |
self.assertEqual(p.__name__, 'not_fail') |
| 256 |
|
| 257 |
def test_property_set_name_incorrect_args(self): |
| 258 |
p = property() |
| 259 |
|
| 260 |
for i in (0, 1, 3): |
| 261 |
with self.assertRaisesRegex( |
| 262 |
TypeError, |
| 263 |
fr'^__set_name__\(\) takes 2 positional arguments but {i} were given$' |
| 264 |
): |
| 265 |
p.__set_name__(*([0] * i)) |
| 266 |
|
| 267 |
def test_property_setname_on_property_subclass(self): |
| 268 |
# https://github.com/python/cpython/issues/100942 |
| 269 |
# Copy was setting the name field without first |
| 270 |
# verifying that the copy was an actual property |
| 271 |
# instance. As a result, the code below was |
| 272 |
# causing a segfault. |
| 273 |
|
| 274 |
class pro(property): |
| 275 |
def __new__(typ, *args, **kwargs): |
| 276 |
return "abcdef" |
| 277 |
|
| 278 |
class A: |
| 279 |
pass |
| 280 |
|
| 281 |
p = property.__new__(pro) |
| 282 |
p.__set_name__(A, 1) |
| 283 |
np = p.getter(lambda self: 1) |
| 284 |
|
| 285 |
# Issue 5890: subclasses of property do not preserve method __doc__ strings |
| 286 |
class PropertySub(property): |
| 287 |
"""This is a subclass of property""" |
| 288 |
|
| 289 |
class PropertySubWoDoc(property): |
| 290 |
pass |
| 291 |
|
| 292 |
class PropertySubSlots(property): |
| 293 |
"""This is a subclass of property that defines __slots__""" |
| 294 |
__slots__ = () |
| 295 |
|
| 296 |
class PropertySubclassTests(unittest.TestCase): |
| 297 |
|
| 298 |
@support.requires_docstrings |
| 299 |
def test_slots_docstring_copy_exception(self): |
| 300 |
# A special case error that we preserve despite the GH-98963 behavior |
| 301 |
# that would otherwise silently ignore this error. |
| 302 |
# This came from commit b18500d39d791c879e9904ebac293402b4a7cd34 |
| 303 |
# as part of https://bugs.python.org/issue5890 which allowed docs to |
| 304 |
# be set via property subclasses in the first place. |
| 305 |
with self.assertRaises(AttributeError): |
| 306 |
class Foo(object): |
| 307 |
@PropertySubSlots |
| 308 |
def spam(self): |
| 309 |
"""Trying to copy this docstring will raise an exception""" |
| 310 |
return 1 |
| 311 |
|
| 312 |
def test_property_with_slots_no_docstring(self): |
| 313 |
# https://github.com/python/cpython/issues/98963#issuecomment-1574413319 |
| 314 |
class slotted_prop(property): |
| 315 |
__slots__ = ("foo",) |
| 316 |
|
| 317 |
p = slotted_prop() # no AttributeError |
| 318 |
self.assertIsNone(getattr(p, "__doc__", None)) |
| 319 |
|
| 320 |
def undocumented_getter(): |
| 321 |
return 4 |
| 322 |
|
| 323 |
p = slotted_prop(undocumented_getter) # New in 3.12: no AttributeError |
| 324 |
self.assertIsNone(getattr(p, "__doc__", None)) |
| 325 |
|
| 326 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 327 |
"Docstrings are omitted with -O2 and above") |
| 328 |
def test_property_with_slots_docstring_silently_dropped(self): |
| 329 |
# https://github.com/python/cpython/issues/98963#issuecomment-1574413319 |
| 330 |
class slotted_prop(property): |
| 331 |
__slots__ = ("foo",) |
| 332 |
|
| 333 |
p = slotted_prop(doc="what's up") # no AttributeError |
| 334 |
self.assertIsNone(p.__doc__) |
| 335 |
|
| 336 |
def documented_getter(): |
| 337 |
"""getter doc.""" |
| 338 |
return 4 |
| 339 |
|
| 340 |
# Historical behavior: A docstring from a getter always raises. |
| 341 |
# (matches test_slots_docstring_copy_exception above). |
| 342 |
with self.assertRaises(AttributeError): |
| 343 |
p = slotted_prop(documented_getter) |
| 344 |
|
| 345 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 346 |
"Docstrings are omitted with -O2 and above") |
| 347 |
def test_property_with_slots_and_doc_slot_docstring_present(self): |
| 348 |
# https://github.com/python/cpython/issues/98963#issuecomment-1574413319 |
| 349 |
class slotted_prop(property): |
| 350 |
__slots__ = ("foo", "__doc__") |
| 351 |
|
| 352 |
p = slotted_prop(doc="what's up") |
| 353 |
self.assertEqual("what's up", p.__doc__) # new in 3.12: This gets set. |
| 354 |
|
| 355 |
def documented_getter(): |
| 356 |
"""what's up getter doc?""" |
| 357 |
return 4 |
| 358 |
|
| 359 |
p = slotted_prop(documented_getter) |
| 360 |
self.assertEqual("what's up getter doc?", p.__doc__) |
| 361 |
|
| 362 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 363 |
"Docstrings are omitted with -O2 and above") |
| 364 |
def test_issue41287(self): |
| 365 |
|
| 366 |
self.assertEqual(PropertySub.__doc__, "This is a subclass of property", |
| 367 |
"Docstring of `property` subclass is ignored") |
| 368 |
|
| 369 |
doc = PropertySub(None, None, None, "issue 41287 is fixed").__doc__ |
| 370 |
self.assertEqual(doc, "issue 41287 is fixed", |
| 371 |
"Subclasses of `property` ignores `doc` constructor argument") |
| 372 |
|
| 373 |
def getter(x): |
| 374 |
"""Getter docstring""" |
| 375 |
|
| 376 |
def getter_wo_doc(x): |
| 377 |
pass |
| 378 |
|
| 379 |
for ps in property, PropertySub, PropertySubWoDoc: |
| 380 |
doc = ps(getter, None, None, "issue 41287 is fixed").__doc__ |
| 381 |
self.assertEqual(doc, "issue 41287 is fixed", |
| 382 |
"Getter overrides explicit property docstring (%s)" % ps.__name__) |
| 383 |
|
| 384 |
doc = ps(getter, None, None, None).__doc__ |
| 385 |
self.assertEqual(doc, "Getter docstring", "Getter docstring is not picked-up (%s)" % ps.__name__) |
| 386 |
|
| 387 |
doc = ps(getter_wo_doc, None, None, "issue 41287 is fixed").__doc__ |
| 388 |
self.assertEqual(doc, "issue 41287 is fixed", |
| 389 |
"Getter overrides explicit property docstring (%s)" % ps.__name__) |
| 390 |
|
| 391 |
doc = ps(getter_wo_doc, None, None, None).__doc__ |
| 392 |
self.assertIsNone(doc, "Property class doc appears in instance __doc__ (%s)" % ps.__name__) |
| 393 |
|
| 394 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 395 |
"Docstrings are omitted with -O2 and above") |
| 396 |
def test_docstring_copy(self): |
| 397 |
class Foo(object): |
| 398 |
@PropertySub |
| 399 |
def spam(self): |
| 400 |
"""spam wrapped in property subclass""" |
| 401 |
return 1 |
| 402 |
self.assertEqual( |
| 403 |
Foo.spam.__doc__, |
| 404 |
"spam wrapped in property subclass") |
| 405 |
|
| 406 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 407 |
"Docstrings are omitted with -O2 and above") |
| 408 |
def test_docstring_copy2(self): |
| 409 |
""" |
| 410 |
Property tries to provide the best docstring it finds for its instances. |
| 411 |
If a user-provided docstring is available, it is preserved on copies. |
| 412 |
If no docstring is available during property creation, the property |
| 413 |
will utilize the docstring from the getter if available. |
| 414 |
""" |
| 415 |
def getter1(self): |
| 416 |
return 1 |
| 417 |
def getter2(self): |
| 418 |
"""doc 2""" |
| 419 |
return 2 |
| 420 |
def getter3(self): |
| 421 |
"""doc 3""" |
| 422 |
return 3 |
| 423 |
|
| 424 |
# Case-1: user-provided doc is preserved in copies |
| 425 |
# of property with undocumented getter |
| 426 |
p = property(getter1, None, None, "doc-A") |
| 427 |
|
| 428 |
p2 = p.getter(getter2) |
| 429 |
self.assertEqual(p.__doc__, "doc-A") |
| 430 |
self.assertEqual(p2.__doc__, "doc-A") |
| 431 |
|
| 432 |
# Case-2: user-provided doc is preserved in copies |
| 433 |
# of property with documented getter |
| 434 |
p = property(getter2, None, None, "doc-A") |
| 435 |
|
| 436 |
p2 = p.getter(getter3) |
| 437 |
self.assertEqual(p.__doc__, "doc-A") |
| 438 |
self.assertEqual(p2.__doc__, "doc-A") |
| 439 |
|
| 440 |
# Case-3: with no user-provided doc new getter doc |
| 441 |
# takes precedence |
| 442 |
p = property(getter2, None, None, None) |
| 443 |
|
| 444 |
p2 = p.getter(getter3) |
| 445 |
self.assertEqual(p.__doc__, "doc 2") |
| 446 |
self.assertEqual(p2.__doc__, "doc 3") |
| 447 |
|
| 448 |
# Case-4: A user-provided doc is assigned after property construction |
| 449 |
# with documented getter. The doc IS NOT preserved. |
| 450 |
# It's an odd behaviour, but it's a strange enough |
| 451 |
# use case with no easy solution. |
| 452 |
p = property(getter2, None, None, None) |
| 453 |
p.__doc__ = "user" |
| 454 |
p2 = p.getter(getter3) |
| 455 |
self.assertEqual(p.__doc__, "user") |
| 456 |
self.assertEqual(p2.__doc__, "doc 3") |
| 457 |
|
| 458 |
# Case-5: A user-provided doc is assigned after property construction |
| 459 |
# with UNdocumented getter. The doc IS preserved. |
| 460 |
p = property(getter1, None, None, None) |
| 461 |
p.__doc__ = "user" |
| 462 |
p2 = p.getter(getter2) |
| 463 |
self.assertEqual(p.__doc__, "user") |
| 464 |
self.assertEqual(p2.__doc__, "user") |
| 465 |
|
| 466 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 467 |
"Docstrings are omitted with -O2 and above") |
| 468 |
def test_prefer_explicit_doc(self): |
| 469 |
# Issue 25757: subclasses of property lose docstring |
| 470 |
self.assertEqual(property(doc="explicit doc").__doc__, "explicit doc") |
| 471 |
self.assertEqual(PropertySub(doc="explicit doc").__doc__, "explicit doc") |
| 472 |
|
| 473 |
class Foo: |
| 474 |
spam = PropertySub(doc="spam explicit doc") |
| 475 |
|
| 476 |
@spam.getter |
| 477 |
def spam(self): |
| 478 |
"""ignored as doc already set""" |
| 479 |
return 1 |
| 480 |
|
| 481 |
def _stuff_getter(self): |
| 482 |
"""ignored as doc set directly""" |
| 483 |
stuff = PropertySub(doc="stuff doc argument", fget=_stuff_getter) |
| 484 |
|
| 485 |
#self.assertEqual(Foo.spam.__doc__, "spam explicit doc") |
| 486 |
self.assertEqual(Foo.stuff.__doc__, "stuff doc argument") |
| 487 |
|
| 488 |
def test_property_no_doc_on_getter(self): |
| 489 |
# If a property's getter has no __doc__ then the property's doc should |
| 490 |
# be None; test that this is consistent with subclasses as well; see |
| 491 |
# GH-2487 |
| 492 |
class NoDoc: |
| 493 |
@property |
| 494 |
def __doc__(self): |
| 495 |
raise AttributeError |
| 496 |
|
| 497 |
self.assertEqual(property(NoDoc()).__doc__, None) |
| 498 |
self.assertEqual(PropertySub(NoDoc()).__doc__, None) |
| 499 |
|
| 500 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 501 |
"Docstrings are omitted with -O2 and above") |
| 502 |
def test_property_setter_copies_getter_docstring(self): |
| 503 |
class Foo(object): |
| 504 |
def __init__(self): self._spam = 1 |
| 505 |
@PropertySub |
| 506 |
def spam(self): |
| 507 |
"""spam wrapped in property subclass""" |
| 508 |
return self._spam |
| 509 |
@spam.setter |
| 510 |
def spam(self, value): |
| 511 |
"""this docstring is ignored""" |
| 512 |
self._spam = value |
| 513 |
foo = Foo() |
| 514 |
self.assertEqual(foo.spam, 1) |
| 515 |
foo.spam = 2 |
| 516 |
self.assertEqual(foo.spam, 2) |
| 517 |
self.assertEqual( |
| 518 |
Foo.spam.__doc__, |
| 519 |
"spam wrapped in property subclass") |
| 520 |
class FooSub(Foo): |
| 521 |
@Foo.spam.setter |
| 522 |
def spam(self, value): |
| 523 |
"""another ignored docstring""" |
| 524 |
self._spam = 'eggs' |
| 525 |
foosub = FooSub() |
| 526 |
self.assertEqual(foosub.spam, 1) |
| 527 |
foosub.spam = 7 |
| 528 |
self.assertEqual(foosub.spam, 'eggs') |
| 529 |
self.assertEqual( |
| 530 |
FooSub.spam.__doc__, |
| 531 |
"spam wrapped in property subclass") |
| 532 |
|
| 533 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 534 |
"Docstrings are omitted with -O2 and above") |
| 535 |
def test_property_new_getter_new_docstring(self): |
| 536 |
|
| 537 |
class Foo(object): |
| 538 |
@PropertySub |
| 539 |
def spam(self): |
| 540 |
"""a docstring""" |
| 541 |
return 1 |
| 542 |
@spam.getter |
| 543 |
def spam(self): |
| 544 |
"""a new docstring""" |
| 545 |
return 2 |
| 546 |
self.assertEqual(Foo.spam.__doc__, "a new docstring") |
| 547 |
class FooBase(object): |
| 548 |
@PropertySub |
| 549 |
def spam(self): |
| 550 |
"""a docstring""" |
| 551 |
return 1 |
| 552 |
class Foo2(FooBase): |
| 553 |
@FooBase.spam.getter |
| 554 |
def spam(self): |
| 555 |
"""a new docstring""" |
| 556 |
return 2 |
| 557 |
self.assertEqual(Foo.spam.__doc__, "a new docstring") |
| 558 |
|
| 559 |
|
| 560 |
class _PropertyUnreachableAttribute: |
| 561 |
msg_format = None |
| 562 |
obj = None |
| 563 |
cls = None |
| 564 |
|
| 565 |
def _format_exc_msg(self, msg): |
| 566 |
return self.msg_format.format(msg) |
| 567 |
|
| 568 |
@classmethod |
| 569 |
def setUpClass(cls): |
| 570 |
cls.obj = cls.cls() |
| 571 |
|
| 572 |
def test_get_property(self): |
| 573 |
with self.assertRaisesRegex(AttributeError, self._format_exc_msg("has no getter")): |
| 574 |
self.obj.foo |
| 575 |
|
| 576 |
def test_set_property(self): |
| 577 |
with self.assertRaisesRegex(AttributeError, self._format_exc_msg("has no setter")): |
| 578 |
self.obj.foo = None |
| 579 |
|
| 580 |
def test_del_property(self): |
| 581 |
with self.assertRaisesRegex(AttributeError, self._format_exc_msg("has no deleter")): |
| 582 |
del self.obj.foo |
| 583 |
|
| 584 |
|
| 585 |
class PropertyUnreachableAttributeWithName(_PropertyUnreachableAttribute, unittest.TestCase): |
| 586 |
msg_format = r"^property 'foo' of 'PropertyUnreachableAttributeWithName\.cls' object {}$" |
| 587 |
|
| 588 |
class cls: |
| 589 |
foo = property() |
| 590 |
|
| 591 |
|
| 592 |
class PropertyUnreachableAttributeNoName(_PropertyUnreachableAttribute, unittest.TestCase): |
| 593 |
msg_format = r"^property of 'PropertyUnreachableAttributeNoName\.cls' object {}$" |
| 594 |
|
| 595 |
class cls: |
| 596 |
pass |
| 597 |
|
| 598 |
cls.foo = property() |
| 599 |
|
| 600 |
|
| 601 |
if __name__ == '__main__': |
| 602 |
unittest.main() |