Skip to content

Commit

Permalink
fix: error message changed in 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed May 25, 2022
1 parent 172b9f4 commit a5a0e5d
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import sys

import pytest

import env # noqa: F401
from pybind11_tests import ConstructorStats
from pybind11_tests import methods_and_attributes as m

NO_GETTER_MSG = (
"unreadable attribute" if sys.version_info < (3, 11) else "object has no getter"
)
NO_SETTER_MSG = (
"can't set attribute" if sys.version_info < (3, 11) else "object has no setter"
)
NO_DELETER_MSG = (
"can't delete attribute" if sys.version_info < (3, 11) else "object has no deleter"
)


def test_methods_and_attributes():
instance1 = m.ExampleMandA()
Expand Down Expand Up @@ -102,47 +114,47 @@ def test_properties():

with pytest.raises(AttributeError) as excinfo:
dummy = instance.def_property_writeonly # unused var
assert "unreadable attribute" in str(excinfo.value)
assert NO_GETTER_MSG in str(excinfo.value)

instance.def_property_writeonly = 4
assert instance.def_property_readonly == 4

with pytest.raises(AttributeError) as excinfo:
dummy = instance.def_property_impossible # noqa: F841 unused var
assert "unreadable attribute" in str(excinfo.value)
assert NO_GETTER_MSG in str(excinfo.value)

with pytest.raises(AttributeError) as excinfo:
instance.def_property_impossible = 5
assert "can't set attribute" in str(excinfo.value)
assert NO_SETTER_MSG in str(excinfo.value)


def test_static_properties():
assert m.TestProperties.def_readonly_static == 1
with pytest.raises(AttributeError) as excinfo:
m.TestProperties.def_readonly_static = 2
assert "can't set attribute" in str(excinfo.value)
assert NO_SETTER_MSG in str(excinfo.value)

m.TestProperties.def_readwrite_static = 2
assert m.TestProperties.def_readwrite_static == 2

with pytest.raises(AttributeError) as excinfo:
dummy = m.TestProperties.def_writeonly_static # unused var
assert "unreadable attribute" in str(excinfo.value)
assert NO_GETTER_MSG in str(excinfo.value)

m.TestProperties.def_writeonly_static = 3
assert m.TestProperties.def_readonly_static == 3

assert m.TestProperties.def_property_readonly_static == 3
with pytest.raises(AttributeError) as excinfo:
m.TestProperties.def_property_readonly_static = 99
assert "can't set attribute" in str(excinfo.value)
assert NO_SETTER_MSG in str(excinfo.value)

m.TestProperties.def_property_static = 4
assert m.TestProperties.def_property_static == 4

with pytest.raises(AttributeError) as excinfo:
dummy = m.TestProperties.def_property_writeonly_static
assert "unreadable attribute" in str(excinfo.value)
assert NO_GETTER_MSG in str(excinfo.value)

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

with pytest.raises(AttributeError) as excinfo:
dummy = instance.def_property_writeonly_static # noqa: F841 unused var
assert "unreadable attribute" in str(excinfo.value)
assert NO_GETTER_MSG in str(excinfo.value)

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


def test_static_cls():
Expand Down

0 comments on commit a5a0e5d

Please sign in to comment.