Skip to content

Commit 1526e39

Browse files
cguardiatswast
andauthored
feature: raise error for unknown properties in job config (#446)
* feature: warn about unknown properties in job config * fix: raise error instead of warning * fix: use hasattr instead of __dict__ * fix bad merge * fix system test that sets wrong property Co-authored-by: Tim Swast <[email protected]>
1 parent 2788736 commit 1526e39

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

google/cloud/bigquery/job/base.py

+8
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,14 @@ def __init__(self, job_type, **kwargs):
659659
for prop, val in kwargs.items():
660660
setattr(self, prop, val)
661661

662+
def __setattr__(self, name, value):
663+
"""Override to be able to raise error if an unknown property is being set"""
664+
if not name.startswith("_") and not hasattr(type(self), name):
665+
raise AttributeError(
666+
"Property {} is unknown for {}.".format(name, type(self))
667+
)
668+
super(_JobConfig, self).__setattr__(name, value)
669+
662670
@property
663671
def labels(self):
664672
"""Dict[str, str]: Labels for the job.

tests/system.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
from google.cloud.bigquery.dataset import DatasetReference
7777
from google.cloud.bigquery.table import Table
7878
from google.cloud._helpers import UTC
79-
from google.cloud.bigquery import dbapi
79+
from google.cloud.bigquery import dbapi, enums
8080
from google.cloud import storage
8181

8282
from test_utils.retry import RetryErrors
@@ -1789,10 +1789,8 @@ def test_query_w_wrong_config(self):
17891789
rows = list(Config.CLIENT.query("SELECT 1;").result())
17901790
assert rows[0][0] == 1
17911791

1792-
project = Config.CLIENT.project
1793-
dataset_ref = bigquery.DatasetReference(project, "dset")
17941792
bad_config = LoadJobConfig()
1795-
bad_config.destination = dataset_ref.table("tbl")
1793+
bad_config.source_format = enums.SourceFormat.CSV
17961794
with self.assertRaises(Exception):
17971795
Config.CLIENT.query(good_query, job_config=bad_config).result()
17981796

tests/unit/job/test_base.py

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from google.api_core import exceptions
2020
import google.api_core.retry
2121
import mock
22+
import pytest
2223

2324
from .helpers import _make_client
2425
from .helpers import _make_connection
@@ -1021,6 +1022,12 @@ def test_ctor(self):
10211022
self.assertEqual(job_config._job_type, self.JOB_TYPE)
10221023
self.assertEqual(job_config._properties, {self.JOB_TYPE: {}})
10231024

1025+
def test_ctor_with_unknown_property_raises_error(self):
1026+
error_text = "Property wrong_name is unknown for"
1027+
with pytest.raises(AttributeError, match=error_text):
1028+
config = self._make_one()
1029+
config.wrong_name = None
1030+
10241031
def test_fill_from_default(self):
10251032
from google.cloud.bigquery import QueryJobConfig
10261033

0 commit comments

Comments
 (0)