Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: add mypy-valid type annotations to samples #1081

Merged
merged 27 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3a681e0
feat: allow cell magic body to be a $variable (#1053)
plamut Nov 16, 2021
1b5dc5c
cleanup: silence non-relevant system test warnings (#1068)
plamut Nov 18, 2021
21cd710
feat: promote `RowIterator.to_arrow_iterable` to public method (#1073)
judahrand Nov 19, 2021
3314dfb
fix: apply timeout to all resumable upload requests (#1070)
plamut Nov 24, 2021
a135956
chore: release 2.31.0 (#1066)
release-please[bot] Dec 2, 2021
be6eb34
test: check extreme DATE/DATETIME values can be loaded from pandas Da…
tswast Dec 2, 2021
effd673
docs: add sample for revoking dataset access (#778)
loferris Dec 6, 2021
d1a9902
Add type hints to magics samples
plamut Nov 27, 2021
8141548
Add type hints to geography samples
plamut Nov 28, 2021
0b58d9d
Add type hints to snippets
plamut Nov 29, 2021
452f15e
Add type hints to top level samples (part 1)
plamut Dec 4, 2021
ee1179a
Add type hints to top level samples (part 2)
plamut Dec 5, 2021
d77d70e
Add type hints to top level samples (part 3)
plamut Dec 7, 2021
8195894
Add type hints to core libraries for samples' needs
plamut Dec 7, 2021
6a3713b
Add top level mypy config and samples session
plamut Dec 8, 2021
c4d0ab5
Add mypy.ini files to samples subdirectories
plamut Dec 8, 2021
c0ef9a0
Enforce late evaluation of some annotations
plamut Dec 8, 2021
86dd389
Install typing.TypedDict backport
plamut Dec 8, 2021
278dac7
Make sure typing.TypedDict is available in samples
plamut Dec 8, 2021
2c78c72
Use import fallback for TypedDict
plamut Dec 8, 2021
12e118d
Fix service account fixture in snippets
plamut Dec 8, 2021
44221f4
chore: update python-docs-samples link to main branch (#1082)
gcf-owl-bot[bot] Dec 9, 2021
eaaffb7
Merge branch 'main' into iss-1074
plamut Dec 14, 2021
040a21c
Add missing annotations to new samples
plamut Dec 14, 2021
fdabea3
Move the override_values default check out
plamut Dec 14, 2021
484a6ee
Get rid of type cast of an optional int property
plamut Dec 14, 2021
8c40ec7
Simplify external config type annotations
plamut Dec 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __init__(
default_query_job_config=None,
client_info=None,
client_options=None,
):
) -> None:
super(Client, self).__init__(
project=project,
credentials=credentials,
Expand Down Expand Up @@ -3426,7 +3426,9 @@ def insert_rows_json(
self,
table: Union[Table, TableReference, TableListItem, str],
json_rows: Sequence[Dict],
row_ids: Union[Iterable[str], AutoRowIDs, None] = AutoRowIDs.GENERATE_UUID,
row_ids: Union[
Iterable[Optional[str]], AutoRowIDs, None
] = AutoRowIDs.GENERATE_UUID,
skip_invalid_rows: bool = None,
ignore_unknown_values: bool = None,
template_suffix: str = None,
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration


def _get_table_reference(self, table_id):
def _get_table_reference(self, table_id: str) -> TableReference:
"""Constructs a TableReference.

Args:
Expand Down Expand Up @@ -144,7 +144,7 @@ class AccessEntry(object):
)
"""Allowed entity types."""

def __init__(self, role, entity_type, entity_id):
def __init__(self, role, entity_type, entity_id) -> None:
if entity_type not in self.ENTITY_TYPES:
message = "Entity type %r not among: %s" % (
entity_type,
Expand Down Expand Up @@ -407,7 +407,7 @@ class Dataset(object):
"default_encryption_configuration": "defaultEncryptionConfiguration",
}

def __init__(self, dataset_ref):
def __init__(self, dataset_ref) -> None:
if isinstance(dataset_ref, str):
dataset_ref = DatasetReference.from_string(dataset_ref)
self._properties = {"datasetReference": dataset_ref.to_api_repr(), "labels": {}}
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/bigquery/encryption_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EncryptionConfiguration(object):
kms_key_name (str): resource ID of Cloud KMS key used for encryption
"""

def __init__(self, kms_key_name=None):
def __init__(self, kms_key_name=None) -> None:
self._properties = {}
if kms_key_name is not None:
self._properties["kmsKeyName"] = kms_key_name
Expand Down
8 changes: 4 additions & 4 deletions google/cloud/bigquery/external_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import base64
import copy
from typing import FrozenSet, Iterable, Optional, Union
from typing import Any, Dict, FrozenSet, Iterable, Optional, Union

from google.cloud.bigquery._helpers import _to_bytes
from google.cloud.bigquery._helpers import _bytes_to_json
Expand Down Expand Up @@ -572,8 +572,8 @@ class HivePartitioningOptions(object):
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#HivePartitioningOptions
"""

def __init__(self):
self._properties = {}
def __init__(self) -> None:
self._properties: Dict[str, Any] = {}

@property
def mode(self):
Expand Down Expand Up @@ -654,7 +654,7 @@ class ExternalConfig(object):
See :attr:`source_format`.
"""

def __init__(self, source_format):
def __init__(self, source_format) -> None:
self._properties = {"sourceFormat": source_format}

@property
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/bigquery/job/copy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CopyJobConfig(_JobConfig):
the property name as the name of a keyword argument.
"""

def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
super(CopyJobConfig, self).__init__("copy", **kwargs)

@property
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/bigquery/job/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LoadJobConfig(_JobConfig):
the property name as the name of a keyword argument.
"""

def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
super(LoadJobConfig, self).__init__("load", **kwargs)

@property
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/bigquery/job/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class QueryJobConfig(_JobConfig):
the property name as the name of a keyword argument.
"""

def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
super(QueryJobConfig, self).__init__("query", **kwargs)

@property
Expand Down Expand Up @@ -1067,7 +1067,7 @@ def ddl_target_table(self):
return prop

@property
def num_dml_affected_rows(self):
def num_dml_affected_rows(self) -> Optional[int]:
"""Return the number of DML rows affected by the job.

See:
Expand Down
11 changes: 7 additions & 4 deletions google/cloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class ArrayQueryParameter(_AbstractQueryParameter):
values (List[appropriate type]): The parameter array values.
"""

def __init__(self, name, array_type, values):
def __init__(self, name, array_type, values) -> None:
self.name = name
self.values = values

Expand Down Expand Up @@ -683,10 +683,13 @@ class StructQueryParameter(_AbstractQueryParameter):
]]): The sub-parameters for the struct
"""

def __init__(self, name, *sub_params):
def __init__(self, name, *sub_params) -> None:
self.name = name
types = self.struct_types = OrderedDict()
values = self.struct_values = {}
self.struct_types: Dict[str, Any] = OrderedDict()
self.struct_values: Dict[str, Any] = {}

types = self.struct_types
values = self.struct_values
for sub in sub_params:
if isinstance(sub, self.__class__):
types[sub.name] = "STRUCT"
Expand Down
10 changes: 5 additions & 5 deletions google/cloud/bigquery/routine/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""Define resources for the BigQuery Routines API."""

from typing import Optional
from typing import Any, Dict, Optional

import google.cloud._helpers # type: ignore
from google.cloud.bigquery import _helpers
Expand Down Expand Up @@ -69,7 +69,7 @@ class Routine(object):
"determinism_level": "determinismLevel",
}

def __init__(self, routine_ref, **kwargs):
def __init__(self, routine_ref, **kwargs) -> None:
if isinstance(routine_ref, str):
routine_ref = RoutineReference.from_string(routine_ref)

Expand Down Expand Up @@ -214,7 +214,7 @@ def return_type(self, value: StandardSqlDataType):
self._properties[self._PROPERTY_TO_API_FIELD["return_type"]] = resource

@property
def return_table_type(self) -> StandardSqlTableType:
def return_table_type(self) -> Optional[StandardSqlTableType]:
"""The return type of a Table Valued Function (TVF) routine.

.. versionadded:: 2.22.0
Expand Down Expand Up @@ -352,8 +352,8 @@ class RoutineArgument(object):
"mode": "mode",
}

def __init__(self, **kwargs):
self._properties = {}
def __init__(self, **kwargs) -> None:
self._properties: Dict[str, Any] = {}
for property_name in kwargs:
setattr(self, property_name, kwargs[property_name])

Expand Down
14 changes: 7 additions & 7 deletions google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class Table(_TableBase):
"require_partition_filter": "requirePartitionFilter",
}

def __init__(self, table_ref, schema=None):
def __init__(self, table_ref, schema=None) -> None:
table_ref = _table_arg_to_table_ref(table_ref)
self._properties = {"tableReference": table_ref.to_api_repr(), "labels": {}}
# Let the @property do validation.
Expand Down Expand Up @@ -1318,7 +1318,7 @@ class Row(object):
# Choose unusual field names to try to avoid conflict with schema fields.
__slots__ = ("_xxx_values", "_xxx_field_to_index")

def __init__(self, values, field_to_index):
def __init__(self, values, field_to_index) -> None:
self._xxx_values = values
self._xxx_field_to_index = field_to_index

Expand Down Expand Up @@ -2230,7 +2230,7 @@ class PartitionRange(object):
Private. Used to construct object from API resource.
"""

def __init__(self, start=None, end=None, interval=None, _properties=None):
def __init__(self, start=None, end=None, interval=None, _properties=None) -> None:
if _properties is None:
_properties = {}
self._properties = _properties
Expand Down Expand Up @@ -2305,10 +2305,10 @@ class RangePartitioning(object):
Private. Used to construct object from API resource.
"""

def __init__(self, range_=None, field=None, _properties=None):
def __init__(self, range_=None, field=None, _properties=None) -> None:
if _properties is None:
_properties = {}
self._properties = _properties
self._properties: Dict[str, Any] = _properties

if range_ is not None:
self.range_ = range_
Expand Down Expand Up @@ -2414,8 +2414,8 @@ class TimePartitioning(object):

def __init__(
self, type_=None, field=None, expiration_ms=None, require_partition_filter=None
):
self._properties = {}
) -> None:
self._properties: Dict[str, Any] = {}
if type_ is None:
self.type_ = TimePartitioningType.DAY
else:
Expand Down
23 changes: 23 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"lint_setup_py",
"blacken",
"mypy",
"mypy_samples",
"pytype",
"docs",
]
Expand Down Expand Up @@ -186,6 +187,28 @@ def system(session):
session.run("py.test", "--quiet", os.path.join("tests", "system"), *session.posargs)


@nox.session(python=DEFAULT_PYTHON_VERSION)
def mypy_samples(session):
"""Run type checks with mypy."""
session.install("-e", ".[all]")

session.install("ipython", "pytest")
session.install(MYPY_VERSION)

# Just install the dependencies' type info directly, since "mypy --install-types"
# might require an additional pass.
session.install("types-mock", "types-pytz")
session.install("typing-extensions") # for TypedDict in pre-3.8 Python versions

session.run(
"mypy",
"--config-file",
str(CURRENT_DIRECTORY / "samples" / "mypy.ini"),
"--no-incremental", # Required by warn-unused-configs from mypy.ini to work
"samples/",
)


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def snippets(session):
"""Run the snippets test suite."""
Expand Down
2 changes: 1 addition & 1 deletion samples/add_empty_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def add_empty_column(table_id):
def add_empty_column(table_id: str) -> None:

# [START bigquery_add_empty_column]
from google.cloud import bigquery
Expand Down
14 changes: 8 additions & 6 deletions samples/browse_table_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def browse_table_data(table_id):
def browse_table_data(table_id: str) -> None:

# [START bigquery_browse_table]

Expand Down Expand Up @@ -41,15 +41,17 @@ def browse_table_data(table_id):
table = client.get_table(table_id) # Make an API request.
fields = table.schema[:2] # First two columns.
rows_iter = client.list_rows(table_id, selected_fields=fields, max_results=10)
rows = list(rows_iter)
print("Selected {} columns from table {}.".format(len(rows_iter.schema), table_id))

rows = list(rows_iter)
print("Downloaded {} rows from table {}".format(len(rows), table_id))

# Print row data in tabular format.
rows = client.list_rows(table, max_results=10)
format_string = "{!s:<16} " * len(rows.schema)
field_names = [field.name for field in rows.schema]
rows_iter = client.list_rows(table, max_results=10)
format_string = "{!s:<16} " * len(rows_iter.schema)
field_names = [field.name for field in rows_iter.schema]
print(format_string.format(*field_names)) # Prints column headers.
for row in rows:

for row in rows_iter:
print(format_string.format(*row)) # Prints row data.
# [END bigquery_browse_table]
2 changes: 1 addition & 1 deletion samples/client_list_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def client_list_jobs():
def client_list_jobs() -> None:

# [START bigquery_list_jobs]

Expand Down
2 changes: 1 addition & 1 deletion samples/client_load_partitioned_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def client_load_partitioned_table(table_id):
def client_load_partitioned_table(table_id: str) -> None:

# [START bigquery_load_table_partitioned]
from google.cloud import bigquery
Expand Down
2 changes: 1 addition & 1 deletion samples/client_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def client_query():
def client_query() -> None:

# [START bigquery_query]

Expand Down
2 changes: 1 addition & 1 deletion samples/client_query_add_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def client_query_add_column(table_id):
def client_query_add_column(table_id: str) -> None:

# [START bigquery_add_column_query_append]
from google.cloud import bigquery
Expand Down
16 changes: 12 additions & 4 deletions samples/client_query_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import typing

if typing.TYPE_CHECKING:
from google.cloud import bigquery

def client_query_batch():

def client_query_batch() -> "bigquery.QueryJob":

# [START bigquery_query_batch]
from google.cloud import bigquery
Expand All @@ -37,9 +42,12 @@ def client_query_batch():

# Check on the progress by getting the job's updated state. Once the state
# is `DONE`, the results are ready.
query_job = client.get_job(
query_job.job_id, location=query_job.location
) # Make an API request.
query_job = typing.cast(
"bigquery.QueryJob",
client.get_job(
query_job.job_id, location=query_job.location
), # Make an API request.
)

print("Job {} is currently in state {}".format(query_job.job_id, query_job.state))
# [END bigquery_query_batch]
Expand Down
2 changes: 1 addition & 1 deletion samples/client_query_destination_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def client_query_destination_table(table_id):
def client_query_destination_table(table_id: str) -> None:

# [START bigquery_query_destination_table]
from google.cloud import bigquery
Expand Down
2 changes: 1 addition & 1 deletion samples/client_query_destination_table_clustered.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def client_query_destination_table_clustered(table_id):
def client_query_destination_table_clustered(table_id: str) -> None:

# [START bigquery_query_clustered_table]
from google.cloud import bigquery
Expand Down
Loading