GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: adds condition class and assoc. unit tests by chalmerlowe · Pull Request #2159 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Apr 10, 2025

Copy link
Copy Markdown
Collaborator

Adds a Condition class and associated suite of tests.

🦕

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chalmerlowe
feat: adds condition class and assoc. unit tests
4e20423
@product-auto-label product-auto-label Bot added size: m Pull request size is medium. api: bigquery Issues related to the googleapis/python-bigquery API. labels Apr 10, 2025
@chalmerlowe
Merge branch 'main' into feat-b330869964-add-dataset-condition-access…
bbfb818
…-policy-version
@chalmerlowe chalmerlowe assigned Linchin Apr 10, 2025
@chalmerlowe
chalmerlowe marked this pull request as ready for review April 11, 2025 11:45
@chalmerlowe
chalmerlowe requested review from a team and whuffman36 April 11, 2025 11:45
@chalmerlowe
Merge branch 'main' into feat-b330869964-add-dataset-condition-access…
5bc95ee
…-policy-version
Linchin
Linchin reviewed Apr 11, 2025
View reviewed changes
Comment thread google/cloud/bigquery/dataset.py
"""str: The expression string for the condition."""

# Cast assumes expression is always set due to __init__ validation
return typing.cast(str, self._properties.get("expression"))

@Linchin Linchin Apr 11, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Just for my education, why is typing.cast() necessary for expression, but not title or description?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chalmerlowe chalmerlowe Apr 15, 2025

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Both mypy and pytype struggle with correctly assessing return types. In this case, both checkers think the return type should be Optional[Any] when we expect (and validate for) a str.

Despite the fact that we indicate via typehinting:

  • that expression is a str in the __init__() method
  • the expression getter returns a str
  • the expression setter method signature requires that value be input as a str
  • the setter method has internal checks to ensure that the value stored in the _properties dict is a str

neither mypy nor pytype will believe it.

It is not inherently clear why they believe our typehinting for title and description but not expression. I have come across this failure multiple times in adding objects to our repos.

There are approximately seven similar examples of this problem elsewhere in this file (that predate my taking over the repo) (and there are examples in other files in this codebase).

These are the error messages we see:

mypy

google/cloud/bigquery/dataset.py:1140: error: Incompatible return value type (got "Optional[Any]", expected "str")  [return-value]

pytype

/repo/python-bigquery/google/cloud/bigquery/dataset.py:1140:1: error: in expression: bad option 'None' in return type [bad-return-type]
           Expected: str
  Actually returned: Optional[Any]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

leahecole reacted with confused emoji
All reactions
  • 😕 1 reaction
Linchin
Linchin reviewed Apr 11, 2025
View reviewed changes
Comment thread tests/unit/test_dataset.py
condition = Condition(expression=self.EXPRESSION)
expected_api_repr = {
"expression": self.EXPRESSION,
"title": None,

@Linchin Linchin Apr 11, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm not very familiar with Condition, do we need to distinguish between title and description being not set versus being an empty string?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chalmerlowe chalmerlowe Apr 15, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I added a pair of asserts to test_setters to confirm the use case that assigning empty strings is allowed and succeeds.

I think there are two issues here:

  1. this test is ensuring that if I try to create a Condition object using ONLY the required expression argument and leave the other arguments (title, description) blank, will it create the expected outcome: i.e. a create an object with None values assigned to both title and description?

  2. Do we need to test how the setter for title and description handle a range of values (None, empty string, and non-empty string, something besides the above, etc)

  • In test_setters we confirm that we can reassign a new string to either title or description AND we confirm that we can assign a new value of None (WAI)
  • In test_constructor_and_getters_full we confirm that we can set a non-empty string (WAI)
  • In test_validation_setters we confirm that something besides a string will fail (WAI)

Our logic does not hinge on whether a value assigned to title or description is Falsey OR not. We do not do a boolean check NOR do empty strings trigger some logic so there is no difference between assigning an empty string vs a non-empty string. I am not convinced that we need the test for empty strings, but I don't believe it will hurt us.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Linchin reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Linchin
Linchin reviewed Apr 11, 2025
View reviewed changes
Comment thread google/cloud/bigquery/dataset.py

def to_api_repr(self) -> Dict[str, Any]:
"""Construct the API resource representation of this Condition."""
return self._properties

@Linchin Linchin Apr 11, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We might want to make a deep copy of the dict, similar to other classes, such as Table.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chalmerlowe chalmerlowe Apr 15, 2025

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Unless we have a compelling reason to use deepcopy, I am disinclined. Thoughts?

In a previous experimental PR, Tim left a comment about some of the pitfalls of using deep copy, if not necessary.

It might be a bit wasteful to make deepcopy here and in `from_api_repr`. Indeed it's safer, but could add a lot of overhead. IIRC we actually removed some `deepcopy` calls from `SchemaField` because it was slowing down customers who build dynamic schemas in their code.

See: #6 and #26

In all cases we are returning a dict (thus there should not be a significant risk of some underlying nested value being changed by another expression in the code)

I was curious about the overall cost of a deepcopy so I did a fairly simple experiment:

In [1]: import copy
In [2]: api_repr = {
   ...:      "expression": "some_value",
   ...:      "title": "some_value",
   ...:      "unexpected_field": "some_value",
   ...: }
In [3]: _properties['condition'] = api_repr

From fastest to slowest:

Simply assign an alias to the dict

In [4]: %timeit new_prop = _properties
14.4 ns ± 0.0149 ns per loop (mean ± std. dev. of 7 runs, 100,000,000 loops each)

# NOTE: when we use `return _properties`, it is the same as using an alias.

Use the builtin copy method in the dict class

This creates a shallow copy of the dict

In [11]: %timeit new_prop = _properties.copy()
58.6 ns ± 0.228 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Create a copy.copy

This also creates a shallow copy of the dict

In [9]: %timeit new_prop = copy.copy(_properties)
145 ns ± 0.401 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Create a copy.deepcopy

In [5]: %timeit new_prop = copy.deepcopy(_properties)
3 µs ± 16.7 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chalmerlowe
Updates two test cases for empty string
5205e45
leahecole
leahecole approved these changes Apr 15, 2025
View reviewed changes
@chalmerlowe
chalmerlowe merged commit a69d6b7 into main Apr 16, 2025
@chalmerlowe
chalmerlowe deleted the feat-b330869964-add-dataset-condition-access-policy-version branch April 16, 2025 09:20
@release-please release-please Bot mentioned this pull request Apr 16, 2025
chore(main): release 3.32.0 #2152
Merged
This was referenced May 20, 2025
May 19, 2025 kitta65/bq-extension-vscode#764
Closed
May 19, 2025 kitta65/prettier-plugin-bq#656
Closed
May 19, 2025 kitta65/bq2cst#515
Closed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@whuffman36 whuffman36 Awaiting requested review from whuffman36
2 more reviewers
@Linchin Linchin Linchin left review comments
@leahecole leahecole leahecole approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@Linchin Linchin

Labels

api: bigquery Issues related to the googleapis/python-bigquery API. size: m Pull request size is medium.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@chalmerlowe @leahecole @Linchin
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.