Skip to content

Commit a5a0e5d

Browse files
committed
fix: error message changed in 3.11
1 parent 172b9f4 commit a5a0e5d

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

tests/test_methods_and_attributes.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import sys
2+
13
import pytest
24

35
import env # noqa: F401
46
from pybind11_tests import ConstructorStats
57
from pybind11_tests import methods_and_attributes as m
68

9+
NO_GETTER_MSG = (
10+
"unreadable attribute" if sys.version_info < (3, 11) else "object has no getter"
11+
)
12+
NO_SETTER_MSG = (
13+
"can't set attribute" if sys.version_info < (3, 11) else "object has no setter"
14+
)
15+
NO_DELETER_MSG = (
16+
"can't delete attribute" if sys.version_info < (3, 11) else "object has no deleter"
17+
)
18+
719

820
def test_methods_and_attributes():
921
instance1 = m.ExampleMandA()
@@ -102,47 +114,47 @@ def test_properties():
102114

103115
with pytest.raises(AttributeError) as excinfo:
104116
dummy = instance.def_property_writeonly # unused var
105-
assert "unreadable attribute" in str(excinfo.value)
117+
assert NO_GETTER_MSG in str(excinfo.value)
106118

107119
instance.def_property_writeonly = 4
108120
assert instance.def_property_readonly == 4
109121

110122
with pytest.raises(AttributeError) as excinfo:
111123
dummy = instance.def_property_impossible # noqa: F841 unused var
112-
assert "unreadable attribute" in str(excinfo.value)
124+
assert NO_GETTER_MSG in str(excinfo.value)
113125

114126
with pytest.raises(AttributeError) as excinfo:
115127
instance.def_property_impossible = 5
116-
assert "can't set attribute" in str(excinfo.value)
128+
assert NO_SETTER_MSG in str(excinfo.value)
117129

118130

119131
def test_static_properties():
120132
assert m.TestProperties.def_readonly_static == 1
121133
with pytest.raises(AttributeError) as excinfo:
122134
m.TestProperties.def_readonly_static = 2
123-
assert "can't set attribute" in str(excinfo.value)
135+
assert NO_SETTER_MSG in str(excinfo.value)
124136

125137
m.TestProperties.def_readwrite_static = 2
126138
assert m.TestProperties.def_readwrite_static == 2
127139

128140
with pytest.raises(AttributeError) as excinfo:
129141
dummy = m.TestProperties.def_writeonly_static # unused var
130-
assert "unreadable attribute" in str(excinfo.value)
142+
assert NO_GETTER_MSG in str(excinfo.value)
131143

132144
m.TestProperties.def_writeonly_static = 3
133145
assert m.TestProperties.def_readonly_static == 3
134146

135147
assert m.TestProperties.def_property_readonly_static == 3
136148
with pytest.raises(AttributeError) as excinfo:
137149
m.TestProperties.def_property_readonly_static = 99
138-
assert "can't set attribute" in str(excinfo.value)
150+
assert NO_SETTER_MSG in str(excinfo.value)
139151

140152
m.TestProperties.def_property_static = 4
141153
assert m.TestProperties.def_property_static == 4
142154

143155
with pytest.raises(AttributeError) as excinfo:
144156
dummy = m.TestProperties.def_property_writeonly_static
145-
assert "unreadable attribute" in str(excinfo.value)
157+
assert NO_GETTER_MSG in str(excinfo.value)
146158

147159
m.TestProperties.def_property_writeonly_static = 5
148160
assert m.TestProperties.def_property_static == 5
@@ -160,7 +172,7 @@ def test_static_properties():
160172

161173
with pytest.raises(AttributeError) as excinfo:
162174
dummy = instance.def_property_writeonly_static # noqa: F841 unused var
163-
assert "unreadable attribute" in str(excinfo.value)
175+
assert NO_GETTER_MSG in str(excinfo.value)
164176

165177
instance.def_property_writeonly_static = 4
166178
assert instance.def_property_static == 4
@@ -180,7 +192,7 @@ def test_static_properties():
180192
properties_override = m.TestPropertiesOverride()
181193
with pytest.raises(AttributeError) as excinfo:
182194
del properties_override.def_readonly
183-
assert "can't delete attribute" in str(excinfo.value)
195+
assert NO_DELETER_MSG in str(excinfo.value)
184196

185197

186198
def test_static_cls():

0 commit comments

Comments
 (0)