Repository: googleapis/python-bigquery

Issue:
Make Model constructor robust against new model types
Follow-up to https://github.com/googleapis/python-bigquery/issues/293

[model_type](https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.model.Model.html#google.cloud.bigquery.model.Model.model_type) appears to be the only enum. Maybe we pull this out of the `_proto` resource and save it as a string, instead?

Alternatively, maybe if we fail to parse, we can set to MODEL_TYPE_UNSPECIFIED?

Discussion:

Patch:
diff --git a/google/cloud/bigquery/model.py b/google/cloud/bigquery/model.py
index 1143b71f..0f5d8f83 100644
--- a/google/cloud/bigquery/model.py
+++ b/google/cloud/bigquery/model.py
@@ -305,9 +305,15 @@ class Model(object):
             start_time = datetime_helpers.from_microseconds(1e3 * float(start_time))
             training_run["startTime"] = datetime_helpers.to_rfc3339(start_time)
 
-        this._proto = json_format.ParseDict(
-            resource, types.Model()._pb, ignore_unknown_fields=True
-        )
+        try:
+            this._proto = json_format.ParseDict(
+                resource, types.Model()._pb, ignore_unknown_fields=True
+            )
+        except json_format.ParseError:
+            resource["modelType"] = "MODEL_TYPE_UNSPECIFIED"
+            this._proto = json_format.ParseDict(
+                resource, types.Model()._pb, ignore_unknown_fields=True
+            )
         return this
 
     def _build_resource(self, filter_fields):


Test patch:
diff --git a/tests/unit/model/test_model.py b/tests/unit/model/test_model.py
index 9fa29a49..8f0bf58d 100644
--- a/tests/unit/model/test_model.py
+++ b/tests/unit/model/test_model.py
@@ -186,6 +186,23 @@ def test_from_api_repr_w_unknown_fields(target_class):
     assert got._properties is resource
 
 
+def test_from_api_repr_w_unknown_type(target_class):
+    from google.cloud.bigquery import ModelReference
+
+    resource = {
+        "modelReference": {
+            "projectId": "my-project",
+            "datasetId": "my_dataset",
+            "modelId": "my_model",
+        },
+        "modelType": "BE_A_GOOD_ROLE_MODEL",
+    }
+    got = target_class.from_api_repr(resource)
+    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
+    assert got.model_type == 0
+    assert got._properties is resource
+
+
 @pytest.mark.parametrize(
     "resource,filter_fields,expected",
     [