Repository: googleapis/python-bigquery
Pull request #2169 (2025-04-30 10:51:59)

feat: add dataset access policy version attribute
Adds a new attribute (`access_policy_version) to the `Dataset` class.
Adds new unittests and updates existing unittests. 




Patch:
diff --git a/google/cloud/bigquery/dataset.py b/google/cloud/bigquery/dataset.py
index 670fe127..d225b710 100644
--- a/google/cloud/bigquery/dataset.py
+++ b/google/cloud/bigquery/dataset.py
@@ -589,6 +589,7 @@ class Dataset(object):
         "default_rounding_mode": "defaultRoundingMode",
         "resource_tags": "resourceTags",
         "external_catalog_dataset_options": "externalCatalogDatasetOptions",
+        "access_policy_version": "accessPolicyVersion",
     }
 
     def __init__(self, dataset_ref) -> None:
@@ -979,6 +980,16 @@ class Dataset(object):
             self._PROPERTY_TO_API_FIELD["external_catalog_dataset_options"]
         ] = (value.to_api_repr() if value is not None else None)
 
+    @property
+    def access_policy_version(self):
+        return self._properties.get("accessPolicyVersion")
+
+    @access_policy_version.setter
+    def access_policy_version(self, value):
+        if not isinstance(value, int) and value is not None:
+            raise ValueError("Pass an integer, or None")
+        self._properties["accessPolicyVersion"] = value
+
     @classmethod
     def from_string(cls, full_dataset_id: str) -> "Dataset":
         """Construct a dataset from fully-qualified dataset ID.
@@ -1217,8 +1228,8 @@ class Condition(object):
 
         return cls(
             expression=resource["expression"],
-            title=resource.get("title", None),
-            description=resource.get("description", None),
+            title=resource.get("title"),
+            description=resource.get("description"),
         )
 
     def __eq__(self, other: object) -> bool:


Test patch:
diff --git a/tests/unit/test_dataset.py b/tests/unit/test_dataset.py
index 51f1809b..94143082 100644
--- a/tests/unit/test_dataset.py
+++ b/tests/unit/test_dataset.py
@@ -1049,6 +1049,7 @@ class TestDataset(unittest.TestCase):
         self.assertIsNone(dataset.friendly_name)
         self.assertIsNone(dataset.location)
         self.assertEqual(dataset.is_case_insensitive, False)
+        self.assertIsNone(dataset.access_policy_version)
 
     def test_ctor_string(self):
         dataset = self._make_one("some-project.some_dset")
@@ -1423,6 +1424,35 @@ class TestDataset(unittest.TestCase):
         expected = api_repr["externalCatalogDatasetOptions"]
         assert result == expected
 
+    def test_access_policy_version_valid_input(self):
+        dataset = self._make_one(self.DS_REF)
+        # Valid inputs for access_policy_version are currently
+        # ints 1, 2, 3, and None
+        # We rely upon the BQ backend to validate acceptable integer
+        # values, rather than perform that validation in the client.
+        for expected in [1, 2, 3, None]:
+            # set property using setter and integer
+            dataset.access_policy_version = expected
+
+            # check getter and _properties dict
+            assert (
+                dataset.access_policy_version == expected
+            ), f"Expected {expected} but got {dataset.access_policy_version}"
+            assert dataset._properties["accessPolicyVersion"] == expected
+
+    def test_access_policy_version_invalid_input(self):
+        dataset = self._make_one(self.DS_REF)
+        # Valid inputs for access_policy_version are currently
+        # ints 1, 2, 3, and None
+
+        with pytest.raises(ValueError):
+            invalid_value = "a string"
+            dataset.access_policy_version = invalid_value
+
+        with pytest.raises(ValueError):
+            invalid_value = 42.0
+            dataset.access_policy_version = invalid_value
+
 
 class TestDatasetListItem(unittest.TestCase):
     @staticmethod