mirror of
https://github.com/pybind/pybind11.git
synced 2025-01-31 07:10:30 +00:00
added instance check
This commit is contained in:
parent
048b539537
commit
890fcae7d8
@ -1045,13 +1045,18 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
struct Empty {};
|
||||
py::class_<Empty>(m, "EmptyAnnotationClass");
|
||||
|
||||
struct Point {};
|
||||
auto point = py::class_<Point>(m, "Point");
|
||||
point.def(py::init());
|
||||
point.attr_with_type_hint<py::typing::ClassVar<float>>("x");
|
||||
point.attr_with_type_hint<py::typing::ClassVar<py::typing::Dict<py::str, int>>>("dict_str_int")
|
||||
struct Static {};
|
||||
auto static_class = py::class_<Static>(m, "Static");
|
||||
static_class.def(py::init());
|
||||
static_class.attr_with_type_hint<py::typing::ClassVar<float>>("x") = 1.0;
|
||||
static_class.attr_with_type_hint<py::typing::ClassVar<py::typing::Dict<py::str, int>>>("dict_str_int")
|
||||
= py::dict();
|
||||
|
||||
struct Instance {};
|
||||
auto instance = py::class_<Instance>(m, "Instance", py::dynamic_attr());
|
||||
instance.def(py::init());
|
||||
instance.attr_with_type_hint<float>("y");
|
||||
|
||||
m.attr_with_type_hint<py::typing::Final<int>>("CONST_INT") = 3;
|
||||
m.attr("defined_PYBIND11_CPP17") = true;
|
||||
#else
|
||||
|
@ -1127,21 +1127,30 @@ def test_module_attribute_types() -> None:
|
||||
)
|
||||
def test_class_attribute_types() -> None:
|
||||
empty_annotations = get_annotations_helper(m.EmptyAnnotationClass)
|
||||
annotations = get_annotations_helper(m.Point)
|
||||
static_annotations = get_annotations_helper(m.Static)
|
||||
instance_annotations = get_annotations_helper(m.Instance)
|
||||
|
||||
assert empty_annotations is None
|
||||
assert annotations["x"] == "ClassVar[float]"
|
||||
assert annotations["dict_str_int"] == "ClassVar[dict[str, int]]"
|
||||
assert static_annotations["x"] == "ClassVar[float]"
|
||||
assert static_annotations["dict_str_int"] == "ClassVar[dict[str, int]]"
|
||||
|
||||
m.Point.x = 1.0
|
||||
assert m.Point.x == 1.0
|
||||
assert m.Static.x == 1.0
|
||||
|
||||
m.Point.x = 3.0
|
||||
point = m.Point()
|
||||
assert point.x == 3.0
|
||||
m.Static.x = 3.0
|
||||
static = m.Static()
|
||||
assert static.x == 3.0
|
||||
|
||||
point.dict_str_int["hi"] = 3
|
||||
assert m.Point().dict_str_int == {"hi": 3}
|
||||
static.dict_str_int["hi"] = 3
|
||||
assert m.Static().dict_str_int == {"hi": 3}
|
||||
|
||||
assert instance_annotations["y"] == "float"
|
||||
instance1 = m.Instance()
|
||||
instance1.y = 4.0
|
||||
|
||||
instance2 = m.Instance()
|
||||
instance2.y = 5.0
|
||||
|
||||
assert instance1.y != instance2.y
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
|
Loading…
Reference in New Issue
Block a user