Repository: conan-io/conan
Pull request #17052 (2024-09-24 14:09:56)

Honour `check_type` parameter to from CMakeDeps & PkgConfigDeps get_property
Changelog: Omit
Docs: Omit




Patch:
diff --git a/conan/tools/cmake/cmakedeps/cmakedeps.py b/conan/tools/cmake/cmakedeps/cmakedeps.py
index f4b49bd1e..e37f270cc 100644
--- a/conan/tools/cmake/cmakedeps/cmakedeps.py
+++ b/conan/tools/cmake/cmakedeps/cmakedeps.py
@@ -155,7 +155,11 @@ class CMakeDeps(object):
             dep_name) in self.build_context_activated and dep.context == "build" else ""
         dep_comp = f"{str(dep_name)}::{comp_name}" if comp_name else f"{str(dep_name)}"
         try:
-            return self._properties[f"{dep_comp}{build_suffix}"][prop]
+            value = self._properties[f"{dep_comp}{build_suffix}"][prop]
+            if check_type is not None and not isinstance(value, check_type):
+                raise ConanException(
+                    f'The expected type for {prop} is "{check_type.__name__}", but "{type(value).__name__}" was found')
+            return value
         except KeyError:
             return dep.cpp_info.get_property(prop, check_type=check_type) if not comp_name \
                 else dep.cpp_info.components[comp_name].get_property(prop, check_type=check_type)
diff --git a/conan/tools/gnu/pkgconfigdeps.py b/conan/tools/gnu/pkgconfigdeps.py
index 3cd688b15..561085bd0 100644
--- a/conan/tools/gnu/pkgconfigdeps.py
+++ b/conan/tools/gnu/pkgconfigdeps.py
@@ -347,7 +347,11 @@ class _PCGenerator:
         dep_name = dep.ref.name
         dep_comp = f"{str(dep_name)}::{comp_name}" if comp_name else f"{str(dep_name)}"
         try:
-            return self._properties[f"{dep_comp}{self._suffix}"][prop]
+            value = self._properties[f"{dep_comp}{self._suffix}"][prop]
+            if check_type is not None and not isinstance(value, check_type):
+                raise ConanException(
+                    f'The expected type for {prop} is "{check_type.__name__}", but "{type(value).__name__}" was found')
+            return value
         except KeyError:
             return dep.cpp_info.get_property(prop, check_type=check_type) if not comp_name \
                 else dep.cpp_info.components[comp_name].get_property(prop, check_type=check_type)


Test patch:
diff --git a/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py b/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py
index b125b2e1d..93c63ce62 100644
--- a/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py
+++ b/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py
@@ -819,3 +819,27 @@ def test_component_name_same_package():
     # FIXME: Depending on itself, this doesn't look good
     assert 'set_property(TARGET dep::dep APPEND PROPERTY ' \
            'INTERFACE_LINK_LIBRARIES dep::dep)' in cmake_target
+
+
+def test_cmakedeps_set_get_property_checktype():
+    c = TestClient()
+    app = textwrap.dedent("""\
+        from conan import ConanFile
+        from conan.tools.cmake import CMakeDeps
+        class Pkg(ConanFile):
+            name = "app"
+            version = "0.1"
+            settings = "build_type"
+            requires = "dep/0.1"
+            def generate(self):
+                deps = CMakeDeps(self)
+                deps.set_property("dep", "foo", 1)
+                deps.get_property("foo", self.dependencies["dep"], check_type=list)
+                deps.generate()
+            """)
+
+    c.save({"dep/conanfile.py": GenConanfile("dep", "0.1"),
+            "app/conanfile.py": app})
+    c.run("create dep")
+    c.run("create app", assert_error=True)
+    assert 'The expected type for foo is "list", but "int" was found' in c.out