| 1 |
import pytest |
| 2 |
|
| 3 |
def test_property_name_in_error(): |
| 4 |
class A: |
| 5 |
@property |
| 6 |
def my_funny_attribute(self): |
| 7 |
return 1 |
| 8 |
|
| 9 |
unreadable = property() |
| 10 |
@unreadable.setter |
| 11 |
def even_funnier(self, value): |
| 12 |
pass |
| 13 |
|
| 14 |
with pytest.raises(AttributeError) as info: |
| 15 |
A().my_funny_attribute = 1 |
| 16 |
assert "my_funny_attribute" in str(info.value) |
| 17 |
|
| 18 |
with pytest.raises(AttributeError) as info: |
| 19 |
del A().my_funny_attribute |
| 20 |
assert "my_funny_attribute" in str(info.value) |
| 21 |
|
| 22 |
with pytest.raises(AttributeError) as info: |
| 23 |
A().even_funnier |
| 24 |
assert "even_funnier" in str(info.value) |
| 25 |
|
| 26 |
def test_property_name_in_error_setter(): |
| 27 |
class A: |
| 28 |
pass |
| 29 |
p = property(lambda self: 1) |
| 30 |
p.__set_name__(A, "it_propagates") |
| 31 |
p = p.setter(lambda self, value: None) |
| 32 |
with pytest.raises(AttributeError) as info: |
| 33 |
p.__delete__(A()) |
| 34 |
assert "it_propagates" in str(info.value) |
| 35 |
|
| 36 |
def test_property_class_qualname_in_error(): |
| 37 |
class ThisNiceNewFeature: |
| 38 |
@property |
| 39 |
def my_funny_attribute(self): |
| 40 |
return 1 |
| 41 |
|
| 42 |
unreadable = property() |
| 43 |
@unreadable.setter |
| 44 |
def even_funnier(self, value): |
| 45 |
pass |
| 46 |
|
| 47 |
with pytest.raises(AttributeError) as info: |
| 48 |
ThisNiceNewFeature().my_funny_attribute = 1 |
| 49 |
assert str(info.value) == "property 'my_funny_attribute' of 'test_property_class_qualname_in_error.<locals>.ThisNiceNewFeature' object has no setter" |
| 50 |
|
| 51 |
with pytest.raises(AttributeError) as info: |
| 52 |
del ThisNiceNewFeature().my_funny_attribute |
| 53 |
assert "ThisNiceNewFeature" in str(info.value) |
| 54 |
|
| 55 |
with pytest.raises(AttributeError) as info: |
| 56 |
ThisNiceNewFeature().unreadable |
| 57 |
assert "ThisNiceNewFeature" in str(info.value) |
| 58 |
|
| 59 |
|
| 60 |
def test_dont_segfault(): |
| 61 |
|
| 62 |
class pro(property): |
| 63 |
def __new__(typ, *args, **kwargs): |
| 64 |
return "abcdef" |
| 65 |
class A: |
| 66 |
pass |
| 67 |
|
| 68 |
p = property.__new__(pro) |
| 69 |
p.__set_name__(A, 1) |
| 70 |
p.getter(lambda self: 1) |
| 71 |
|
| 72 |
def test_super_error_message(): |
| 73 |
with raises(TypeError) as info: |
| 74 |
super(1, int) |
| 75 |
assert str(info.value) == "super() argument 1 must be a type, not int" |