Skip to content

Commit

Permalink
added instance check
Browse files Browse the repository at this point in the history
  • Loading branch information
InvincibleRMC committed Dec 15, 2024
1 parent 048b539 commit 890fcae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
15 changes: 10 additions & 5 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 19 additions & 10 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 890fcae

Please sign in to comment.