Skip to content

Commit e8f6c87

Browse files
committed
style: apply ruff formatting and fix version helper caching leak
1 parent eac0470 commit e8f6c87

6 files changed

Lines changed: 57 additions & 55 deletions

File tree

packages/google-cloud-bigquery/google/cloud/bigquery/_versions_helpers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ def installed_version(self) -> packaging.version.Version:
263263
try:
264264
import pandas_gbq # type: ignore
265265

266-
return packaging.version.parse(
267-
getattr(pandas_gbq, "__version__", "0.0.0")
268-
)
266+
return packaging.version.parse(getattr(pandas_gbq, "__version__", "0.0.0"))
269267
except Exception:
270268
return packaging.version.parse("0.0.0")
271269

packages/google-cloud-bigquery/google/cloud/bigquery/magics/magics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,9 @@ def _cell_magic(line, query):
496496
raise rebranded_error from exc
497497
except lap.exceptions.ParseError as exc:
498498
rebranded_error = ValueError(
499-
"Unrecognized input, are option values correct? "
500-
"Error details: {}".format(exc.args[0])
499+
"Unrecognized input, are option values correct? Error details: {}".format(
500+
exc.args[0]
501+
)
501502
)
502503
raise rebranded_error from exc
503504

packages/google-cloud-bigquery/google/cloud/bigquery/table.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,9 @@ def biglake_configuration(self, value):
542542
api_repr = value
543543
if value is not None:
544544
api_repr = value.to_api_repr()
545-
self._properties[
546-
self._PROPERTY_TO_API_FIELD["biglake_configuration"]
547-
] = api_repr
545+
self._properties[self._PROPERTY_TO_API_FIELD["biglake_configuration"]] = (
546+
api_repr
547+
)
548548

549549
@property
550550
def require_partition_filter(self):
@@ -558,9 +558,9 @@ def require_partition_filter(self):
558558

559559
@require_partition_filter.setter
560560
def require_partition_filter(self, value):
561-
self._properties[
562-
self._PROPERTY_TO_API_FIELD["require_partition_filter"]
563-
] = value
561+
self._properties[self._PROPERTY_TO_API_FIELD["require_partition_filter"]] = (
562+
value
563+
)
564564

565565
@property
566566
def schema(self):
@@ -658,9 +658,9 @@ def encryption_configuration(self, value):
658658
api_repr = value
659659
if value is not None:
660660
api_repr = value.to_api_repr()
661-
self._properties[
662-
self._PROPERTY_TO_API_FIELD["encryption_configuration"]
663-
] = api_repr
661+
self._properties[self._PROPERTY_TO_API_FIELD["encryption_configuration"]] = (
662+
api_repr
663+
)
664664

665665
@property
666666
def created(self):
@@ -799,7 +799,7 @@ def time_partitioning(self, value):
799799
api_repr = value.to_api_repr()
800800
elif value is not None:
801801
raise ValueError(
802-
"value must be google.cloud.bigquery.table.TimePartitioning " "or None"
802+
"value must be google.cloud.bigquery.table.TimePartitioning or None"
803803
)
804804
self._properties[self._PROPERTY_TO_API_FIELD["time_partitioning"]] = api_repr
805805

@@ -935,9 +935,9 @@ def expires(self, value):
935935
if not isinstance(value, datetime.datetime) and value is not None:
936936
raise ValueError("Pass a datetime, or None")
937937
value_ms = google.cloud._helpers._millis_from_datetime(value)
938-
self._properties[
939-
self._PROPERTY_TO_API_FIELD["expires"]
940-
] = _helpers._str_or_none(value_ms)
938+
self._properties[self._PROPERTY_TO_API_FIELD["expires"]] = (
939+
_helpers._str_or_none(value_ms)
940+
)
941941

942942
@property
943943
def friendly_name(self):
@@ -1133,9 +1133,9 @@ def external_data_configuration(self, value):
11331133
api_repr = value
11341134
if value is not None:
11351135
api_repr = value.to_api_repr()
1136-
self._properties[
1137-
self._PROPERTY_TO_API_FIELD["external_data_configuration"]
1138-
] = api_repr
1136+
self._properties[self._PROPERTY_TO_API_FIELD["external_data_configuration"]] = (
1137+
api_repr
1138+
)
11391139

11401140
@property
11411141
def snapshot_definition(self) -> Optional["SnapshotDefinition"]:
@@ -3038,8 +3038,7 @@ def to_geodataframe(
30383038
)
30393039
if not geography_columns:
30403040
raise TypeError(
3041-
"There must be at least one GEOGRAPHY column"
3042-
" to create a GeoDataFrame"
3041+
"There must be at least one GEOGRAPHY column to create a GeoDataFrame"
30433042
)
30443043

30453044
if geography_column:

packages/google-cloud-bigquery/tests/unit/test__pyarrow_helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def test_bq_to_arrow_scalars(module_under_test):
3535
# but we'd like this to map as closely to the BQ Storage API as
3636
# possible, which uses the string() dtype, as JSON support in Arrow
3737
# predates JSON support in BigQuery by several years.
38-
module_under_test.bq_to_arrow_scalars("JSON")()
39-
== pyarrow.string()
38+
module_under_test.bq_to_arrow_scalars("JSON")() == pyarrow.string()
4039
)
4140
assert module_under_test.bq_to_arrow_scalars("UNKNOWN_TYPE") is None
4241

packages/google-cloud-bigquery/tests/unit/test_magics.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,11 @@ def test_bigquery_magic_with_bqstorage_from_argument(
686686
google.cloud.bigquery.job.QueryJob, instance=True
687687
)
688688
query_job_mock.to_dataframe.return_value = result
689-
with run_query_patch as run_query_mock, (
690-
bqstorage_client_patch
691-
), warnings.catch_warnings(record=True) as warned:
689+
with (
690+
run_query_patch as run_query_mock,
691+
bqstorage_client_patch,
692+
warnings.catch_warnings(record=True) as warned,
693+
):
692694
run_query_mock.return_value = query_job_mock
693695

694696
return_value = ip.run_cell_magic("bigquery", "--use_bqstorage_api", sql)
@@ -854,11 +856,12 @@ def test_bigquery_magic_w_max_results_query_job_results_fails(monkeypatch):
854856
)
855857
query_job_mock.result.side_effect = [[], OSError]
856858

857-
with pytest.raises(
858-
OSError
859-
), client_query_patch as client_query_mock, (
860-
default_patch
861-
), close_transports_patch as close_transports:
859+
with (
860+
pytest.raises(OSError),
861+
client_query_patch as client_query_mock,
862+
default_patch,
863+
close_transports_patch as close_transports,
864+
):
862865
client_query_mock.return_value = query_job_mock
863866
ip.run_cell_magic("bigquery", "--max_results=5", sql)
864867

@@ -1977,9 +1980,10 @@ def test_bigquery_magic_nonexisting_query_variable(monkeypatch):
19771980
ip.user_ns.pop("custom_query", None) # Make sure the variable does NOT exist.
19781981
cell_body = "$custom_query" # Referring to a non-existing variable name.
19791982

1980-
with pytest.raises(
1981-
NameError, match=r".*custom_query does not exist.*"
1982-
), run_query_patch as run_query_mock:
1983+
with (
1984+
pytest.raises(NameError, match=r".*custom_query does not exist.*"),
1985+
run_query_patch as run_query_mock,
1986+
):
19831987
ip.run_cell_magic("bigquery", "", cell_body)
19841988

19851989
run_query_mock.assert_not_called()
@@ -2000,9 +2004,10 @@ def test_bigquery_magic_empty_query_variable_name(monkeypatch):
20002004
)
20012005
cell_body = "$" # Not referring to any variable (name omitted).
20022006

2003-
with pytest.raises(
2004-
NameError, match=r"(?i).*missing query variable name.*"
2005-
), run_query_patch as run_query_mock:
2007+
with (
2008+
pytest.raises(NameError, match=r"(?i).*missing query variable name.*"),
2009+
run_query_patch as run_query_mock,
2010+
):
20062011
ip.run_cell_magic("bigquery", "", cell_body)
20072012

20082013
run_query_mock.assert_not_called()
@@ -2028,9 +2033,10 @@ def test_bigquery_magic_query_variable_non_string(ipython_ns_cleanup, monkeypatc
20282033
ip.user_ns["custom_query"] = object()
20292034
cell_body = "$custom_query" # Referring to a non-string variable.
20302035

2031-
with pytest.raises(
2032-
TypeError, match=r".*must be a string or a bytes-like.*"
2033-
), run_query_patch as run_query_mock:
2036+
with (
2037+
pytest.raises(TypeError, match=r".*must be a string or a bytes-like.*"),
2038+
run_query_patch as run_query_mock,
2039+
):
20342040
ip.run_cell_magic("bigquery", "", cell_body)
20352041

20362042
run_query_mock.assert_not_called()
@@ -2195,9 +2201,11 @@ def test_bigquery_magic_create_dataset_fails(monkeypatch):
21952201
autospec=True,
21962202
)
21972203

2198-
with pytest.raises(
2199-
OSError
2200-
), create_dataset_if_necessary_patch, close_transports_patch as close_transports:
2204+
with (
2205+
pytest.raises(OSError),
2206+
create_dataset_if_necessary_patch,
2207+
close_transports_patch as close_transports,
2208+
):
22012209
ip.run_cell_magic(
22022210
"bigquery",
22032211
"--destination_table dataset_id.table_id",

packages/google-cloud-bigquery/tests/unit/test_table.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,7 @@ def test_from_api_repr(self):
376376
def test___repr__(self):
377377
dataset = DatasetReference("project1", "dataset1")
378378
table1 = self._make_one(dataset, "table1")
379-
expected = (
380-
"TableReference(DatasetReference('project1', 'dataset1'), " "'table1')"
381-
)
379+
expected = "TableReference(DatasetReference('project1', 'dataset1'), 'table1')"
382380
self.assertEqual(repr(table1), expected)
383381

384382
def test___str__(self):
@@ -1831,9 +1829,7 @@ def test___repr__(self):
18311829
dataset = DatasetReference("project1", "dataset1")
18321830
table1 = self._make_one(TableReference(dataset, "table1"))
18331831
expected = (
1834-
"Table(TableReference("
1835-
"DatasetReference('project1', 'dataset1'), "
1836-
"'table1'))"
1832+
"Table(TableReference(DatasetReference('project1', 'dataset1'), 'table1'))"
18371833
)
18381834
self.assertEqual(repr(table1), expected)
18391835

@@ -2873,7 +2869,8 @@ def test__should_use_bqstorage_returns_true_if_no_cached_results(self):
28732869

28742870
def test__should_use_bqstorage_returns_false_if_page_token_set(self):
28752871
iterator = self._make_one(
2876-
page_token="abc", first_page_response=None # not cached
2872+
page_token="abc",
2873+
first_page_response=None, # not cached
28772874
)
28782875
result = iterator._should_use_bqstorage(
28792876
bqstorage_client=None, create_bqstorage_client=True
@@ -2882,7 +2879,8 @@ def test__should_use_bqstorage_returns_false_if_page_token_set(self):
28822879

28832880
def test__should_use_bqstorage_returns_false_if_max_results_set(self):
28842881
iterator = self._make_one(
2885-
max_results=10, first_page_response=None # not cached
2882+
max_results=10,
2883+
first_page_response=None, # not cached
28862884
)
28872885
result = iterator._should_use_bqstorage(
28882886
bqstorage_client=None, create_bqstorage_client=True
@@ -5599,8 +5597,7 @@ def test_to_geodataframe_no_geog(self):
55995597
with self.assertRaisesRegex(
56005598
TypeError,
56015599
re.escape(
5602-
"There must be at least one GEOGRAPHY column"
5603-
" to create a GeoDataFrame"
5600+
"There must be at least one GEOGRAPHY column to create a GeoDataFrame"
56045601
),
56055602
):
56065603
row_iterator.to_geodataframe(create_bqstorage_client=False)

0 commit comments

Comments
 (0)