Skip to content

Commit ad0f33c

Browse files
authored
docs: fix cast method shown on public docs (#2436)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery-dataframes/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕
1 parent 51309df commit ad0f33c

7 files changed

Lines changed: 37 additions & 29 deletions

File tree

‎bigframes/ml/base.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"""
2525

2626
importabc
27-
fromtypingimportcast, Optional, TypeVar, Union
27+
importtyping
28+
fromtypingimportOptional, TypeVar, Union
2829
importwarnings
2930

3031
importbigframes_vendored.sklearn.base
@@ -133,7 +134,7 @@ def register(self: _T, vertex_ai_model_id: Optional[str] = None) -> _T:
133134
self._bqml_model=self._create_bqml_model() # type: ignore
134135
exceptAttributeError:
135136
raiseRuntimeError("A model must be trained before register.")
136-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
137+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
137138

138139
self._bqml_model.register(vertex_ai_model_id)
139140
returnself
@@ -286,7 +287,7 @@ def _predict_and_retry(
286287
bpd.concat([df_result, df_succ]) ifdf_resultisnotNoneelsedf_succ
287288
)
288289

289-
df_result=cast(
290+
df_result=typing.cast(
290291
bpd.DataFrame,
291292
bpd.concat([df_result, df_fail]) ifdf_resultisnotNoneelsedf_fail,
292293
)
@@ -306,7 +307,7 @@ def _extract_output_names(self):
306307

307308
output_names= []
308309
fortransform_colinself._bqml_model._model._properties["transformColumns"]:
309-
transform_col_dict=cast(dict, transform_col)
310+
transform_col_dict=typing.cast(dict, transform_col)
310311
# pass the columns that are not transformed
311312
if"transformSql"notintransform_col_dict:
312313
continue

‎bigframes/ml/compose.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
importre
2222
importtypes
2323
importtyping
24-
fromtypingimportcast, Iterable, List, Optional, Set, Tuple, Union
24+
fromtypingimportIterable, List, Optional, Set, Tuple, Union
2525

2626
frombigframes_vendoredimportconstants
2727
importbigframes_vendored.sklearn.compose._column_transformer
@@ -218,7 +218,7 @@ def camel_to_snake(name):
218218

219219
output_names= []
220220
fortransform_colinbq_model._properties["transformColumns"]:
221-
transform_col_dict=cast(dict, transform_col)
221+
transform_col_dict=typing.cast(dict, transform_col)
222222
# pass the columns that are not transformed
223223
if"transformSql"notintransform_col_dict:
224224
continue
@@ -282,7 +282,7 @@ def _merge(
282282
returnself# SQLScalarColumnTransformer only work inside ColumnTransformer
283283
feature_columns_sorted=sorted(
284284
[
285-
cast(str, feature_column.name)
285+
typing.cast(str, feature_column.name)
286286
forfeature_columninbq_model.feature_columns
287287
]
288288
)

‎bigframes/ml/core.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
importdataclasses
2020
importdatetime
21-
fromtypingimportCallable, cast, Iterable, Mapping, Optional, Union
21+
importtyping
22+
fromtypingimportCallable, Iterable, Mapping, Optional, Union
2223
importuuid
2324

2425
fromgoogle.cloudimportbigquery
@@ -376,7 +377,7 @@ def copy(self, new_model_name: str, replace: bool = False) -> BqmlModel:
376377
defregister(self, vertex_ai_model_id: Optional[str] =None) ->BqmlModel:
377378
ifvertex_ai_model_idisNone:
378379
# vertex id needs to start with letters. https://cloud.google.com/vertex-ai/docs/general/resource-naming
379-
vertex_ai_model_id="bigframes_"+cast(str, self._model.model_id)
380+
vertex_ai_model_id="bigframes_"+typing.cast(str, self._model.model_id)
380381

381382
# truncate as Vertex ID only accepts 63 characters, easily exceeding the limit for temp models.
382383
# The possibility of conflicts should be low.

‎bigframes/ml/imported.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from __future__ importannotations
1818

19-
fromtypingimportcast, Mapping, Optional
19+
importtyping
20+
fromtypingimportMapping, Optional
2021

2122
fromgoogle.cloudimportbigquery
2223

@@ -78,7 +79,7 @@ def predict(self, X: utils.ArrayType) -> bpd.DataFrame:
7879
ifself.model_pathisNone:
7980
raiseValueError("Model GCS path must be provided.")
8081
self._bqml_model=self._create_bqml_model()
81-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
82+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
8283

8384
(X,) =utils.batch_convert_to_dataframe(X)
8485

@@ -99,7 +100,7 @@ def to_gbq(self, model_name: str, replace: bool = False) -> TensorFlowModel:
99100
ifself.model_pathisNone:
100101
raiseValueError("Model GCS path must be provided.")
101102
self._bqml_model=self._create_bqml_model()
102-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
103+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
103104

104105
new_model=self._bqml_model.copy(model_name, replace)
105106
returnnew_model.session.read_gbq_model(model_name)
@@ -157,7 +158,7 @@ def predict(self, X: utils.ArrayType) -> bpd.DataFrame:
157158
ifself.model_pathisNone:
158159
raiseValueError("Model GCS path must be provided.")
159160
self._bqml_model=self._create_bqml_model()
160-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
161+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
161162

162163
(X,) =utils.batch_convert_to_dataframe(X, session=self._bqml_model.session)
163164

@@ -178,7 +179,7 @@ def to_gbq(self, model_name: str, replace: bool = False) -> ONNXModel:
178179
ifself.model_pathisNone:
179180
raiseValueError("Model GCS path must be provided.")
180181
self._bqml_model=self._create_bqml_model()
181-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
182+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
182183

183184
new_model=self._bqml_model.copy(model_name, replace)
184185
returnnew_model.session.read_gbq_model(model_name)
@@ -276,7 +277,7 @@ def predict(self, X: utils.ArrayType) -> bpd.DataFrame:
276277
ifself.model_pathisNone:
277278
raiseValueError("Model GCS path must be provided.")
278279
self._bqml_model=self._create_bqml_model()
279-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
280+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
280281

281282
(X,) =utils.batch_convert_to_dataframe(X, session=self._bqml_model.session)
282283

@@ -297,7 +298,7 @@ def to_gbq(self, model_name: str, replace: bool = False) -> XGBoostModel:
297298
ifself.model_pathisNone:
298299
raiseValueError("Model GCS path must be provided.")
299300
self._bqml_model=self._create_bqml_model()
300-
self._bqml_model=cast(core.BqmlModel, self._bqml_model)
301+
self._bqml_model=typing.cast(core.BqmlModel, self._bqml_model)
301302

302303
new_model=self._bqml_model.copy(model_name, replace)
303304
returnnew_model.session.read_gbq_model(model_name)

‎bigframes/ml/llm.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from __future__ importannotations
1818

19-
fromtypingimportcast, Iterable, Literal, Mapping, Optional, Union
19+
importtyping
20+
fromtypingimportIterable, Literal, Mapping, Optional, Union
2021
importwarnings
2122

2223
importbigframes_vendored.constantsasconstants
@@ -252,7 +253,7 @@ def predict(
252253

253254
iflen(X.columns) ==1:
254255
# BQML identified the column by name
255-
col_label=cast(blocks.Label, X.columns[0])
256+
col_label=typing.cast(blocks.Label, X.columns[0])
256257
X=X.rename(columns={col_label: "content"})
257258

258259
options: dict= {}
@@ -391,7 +392,7 @@ def predict(
391392

392393
iflen(X.columns) ==1:
393394
# BQML identified the column by name
394-
col_label=cast(blocks.Label, X.columns[0])
395+
col_label=typing.cast(blocks.Label, X.columns[0])
395396
X=X.rename(columns={col_label: "content"})
396397

397398
# TODO(garrettwu): remove transform to ObjRefRuntime when BQML supports ObjRef as input
@@ -604,7 +605,10 @@ def fit(
604605
options["prompt_col"] =X.columns.tolist()[0]
605606

606607
self._bqml_model=self._bqml_model_factory.create_llm_remote_model(
607-
X, y, options=options, connection_name=cast(str, self.connection_name)
608+
X,
609+
y,
610+
options=options,
611+
connection_name=typing.cast(str, self.connection_name),
608612
)
609613
returnself
610614

@@ -735,7 +739,7 @@ def predict(
735739

736740
iflen(X.columns) ==1:
737741
# BQML identified the column by name
738-
col_label=cast(blocks.Label, X.columns[0])
742+
col_label=typing.cast(blocks.Label, X.columns[0])
739743
X=X.rename(columns={col_label: "prompt"})
740744

741745
options: dict= {
@@ -820,8 +824,8 @@ def score(
820824
)
821825

822826
# BQML identified the column by name
823-
X_col_label=cast(blocks.Label, X.columns[0])
824-
y_col_label=cast(blocks.Label, y.columns[0])
827+
X_col_label=typing.cast(blocks.Label, X.columns[0])
828+
y_col_label=typing.cast(blocks.Label, y.columns[0])
825829
X=X.rename(columns={X_col_label: "input_text"})
826830
y=y.rename(columns={y_col_label: "output_text"})
827831

@@ -1033,7 +1037,7 @@ def predict(
10331037

10341038
iflen(X.columns) ==1:
10351039
# BQML identified the column by name
1036-
col_label=cast(blocks.Label, X.columns[0])
1040+
col_label=typing.cast(blocks.Label, X.columns[0])
10371041
X=X.rename(columns={col_label: "prompt"})
10381042

10391043
options= {

‎bigframes/ml/model_selection.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
importinspect
2121
fromitertoolsimportchain
2222
importtime
23-
fromtypingimportcast, Generator, List, Optional, Union
23+
importtyping
24+
fromtypingimportGenerator, List, Optional, Union
2425

2526
importbigframes_vendored.sklearn.model_selection._splitasvendored_model_selection_split
2627
importbigframes_vendored.sklearn.model_selection._validationasvendored_model_selection_validation
@@ -99,10 +100,10 @@ def _stratify_split(df: bpd.DataFrame, stratify: bpd.Series) -> List[bpd.DataFra
99100
train_dfs.append(train)
100101
test_dfs.append(test)
101102

102-
train_df=cast(
103+
train_df=typing.cast(
103104
bpd.DataFrame, bpd.concat(train_dfs).drop(columns="bigframes_stratify_col")
104105
)
105-
test_df=cast(
106+
test_df=typing.cast(
106107
bpd.DataFrame, bpd.concat(test_dfs).drop(columns="bigframes_stratify_col")
107108
)
108109
return [train_df, test_df]

‎bigframes/ml/preprocessing.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from __future__ importannotations
1919

2020
importtyping
21-
fromtypingimportcast, Iterable, List, Literal, Optional, Union
21+
fromtypingimportIterable, List, Literal, Optional, Union
2222

2323
importbigframes_vendored.sklearn.preprocessing._data
2424
importbigframes_vendored.sklearn.preprocessing._discretization
@@ -470,7 +470,7 @@ def _parse_from_sql(cls, sql: str) -> tuple[OneHotEncoder, str]:
470470
s=sql[sql.find("(") +1 : sql.find(")")]
471471
col_label, drop_str, top_k, frequency_threshold=s.split(", ")
472472
drop= (
473-
cast(Literal["most_frequent"], "most_frequent")
473+
typing.cast(Literal["most_frequent"], "most_frequent")
474474
ifdrop_str.lower() =="'most_frequent'"
475475
elseNone
476476
)

0 commit comments

Comments
 (0)