Skip to content

Fix: Update type hints for various BigQuery files #2206

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

Merged
merged 3 commits into from
May 30, 2025

Conversation

chalmerlowe
Copy link
Collaborator

This commit addresses several elements of Issue #2132 by updating type hints in the following files:

  • google/cloud/bigquery/external_config.py
  • google/cloud/bigquery/job/base.py
  • google/cloud/bigquery/routine/routine.py
  • google/cloud/bigquery/schema.py
  • google/cloud/bigquery/table.py

These changes improve code clarity and maintainability by providing more accurate type information.

Partially fixes #2132 🦕

This commit addresses Issue #2132 by updating type hints in the following files:

- google/cloud/bigquery/external_config.py
- google/cloud/bigquery/job/base.py
- google/cloud/bigquery/routine/routine.py
- google/cloud/bigquery/schema.py
- google/cloud/bigquery/table.py

These changes improve code clarity and maintainability by providing more accurate type information.
@chalmerlowe chalmerlowe requested review from a team as code owners May 29, 2025 16:56
@chalmerlowe chalmerlowe requested a review from shollyman May 29, 2025 16:56
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: bigquery Issues related to the googleapis/python-bigquery API. labels May 29, 2025
Copy link
Contributor

@shollyman shollyman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly just questions about whether things should be more permissive of optional fallbacks.

prop = self._properties.get("schema", {}) # type: ignore
return [SchemaField.from_api_repr(field) for field in prop.get("fields", [])] # type: ignore
prop: Dict[str, Any] = typing.cast(
Dict[str, Any], self._properties.get("schema", {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Optional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method call has a default value:

If you call .get() you will get back the schema dict OR you will get back an empty dict, so I don't think Optional applies here.

# TODO: The typehinting for this needs work. Setting this pragma to temporarily
# manage a pytype issue that came up in another PR. See Issue: #2132
return self._properties["projectId"] # pytype: disable=typed-dict-error
return self._properties.get("projectId", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be optional / return none as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these (project, dataset_id, routine_id) are used as directly as inputs to a string formatting function. So setting them to None seems slightly more risky than setting them to an empty string. i.e. they should not exist at all (their default state OR be some form of string (empty OR otherwise)).

@property
def path(self):
    """str: URL path for the routine's APIs."""
    return "/projects/%s/datasets/%s/routines/%s" % (
        self.project,
        self.dataset_id,
        self.routine_id,
    )

If we used None, path() could end up producing the following:
"/projects/None/datasets/None/routines/None"
Which might produce undesirable path collisions (not likely, but possible)

Whereas with an empty string we would get:
"/projects//datasets//routines/"
Should result in an error upon receipt by the back end.

@chalmerlowe chalmerlowe merged commit b863291 into main May 30, 2025
27 checks passed
@chalmerlowe chalmerlowe deleted the fix-type-hints-2132 branch May 30, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. size: s Pull request size is small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cleanup tech debt from removing Python 3.7 and 3.8
3 participants