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

feat: Add warning when user tries to access struct series fields with __getitem__ #1082

Merged
merged 19 commits into from
Oct 25, 2024

Conversation

sycai
Copy link
Contributor

@sycai sycai commented Oct 14, 2024

No description provided.

@sycai sycai requested review from a team as code owners October 14, 2024 20:43
@sycai sycai requested a review from shobsi October 14, 2024 20:43
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Oct 14, 2024
@sycai sycai requested a review from tswast October 14, 2024 20:45
with pytest.warns(UserWarning, match=r"Series\.struct\.field\(.+\)"):
s[key]
else:
s[key]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we have a check that a warning is not raised in this case?

I would prefer two separate tests to make this easier to follow. See go/tott/661

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I separated the tests into two parts: 1 for warning and 1 for no warning. Hopefully this makes it easier to read and understand.

session=session,
)

s[key]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make this test fail if a warning is raised.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

session=session,
)

s[key]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make this test fail if a warning is raised.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! Maybe worth a comment about the filterwarnings above, as it'n not 100% obvious to me.

Alternatively, could we use the solution suggested here, instead?

https://docs.pytest.org/en/stable/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests

Suggested change
s[key]
with warnings.catch_warnings():
warnings.simplefilter("error", category=UserWarning)
s[key]

Better yet, have a custom category so we know precisely what warning to fail on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this approach is simpler. Defined a dedicated warning class instead. I also set a stack level as suggested.

@sycai sycai requested a review from tswast October 24, 2024 20:59

if not bigframes.dtypes.is_string_like(series.index.dtype):
warnings.warn(
"Are you trying to access struct fields? If so, please use Series.struct.field(...) method instead."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we do a custom category (defined in bigframes.exceptions) here to make it a bit easier to filter out if needed for some reason?

Also, I recommend setting stacklevel so that the error shows up in user code, not as appearing deep in BigFrames.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have provided a stack level such that the warning call site is now at __getitem__

session=session,
)

s[key]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! Maybe worth a comment about the filterwarnings above, as it'n not 100% obvious to me.

Alternatively, could we use the solution suggested here, instead?

https://docs.pytest.org/en/stable/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests

Suggested change
s[key]
with warnings.catch_warnings():
warnings.simplefilter("error", category=UserWarning)
s[key]

Better yet, have a custom category so we know precisely what warning to fail on.

@sycai sycai requested a review from tswast October 25, 2024 21:33
bigframes/exceptions.py Outdated Show resolved Hide resolved
bigframes/core/indexers.py Outdated Show resolved Hide resolved
tests/system/small/core/test_indexers.py Outdated Show resolved Hide resolved
tests/system/small/core/test_indexers.py Outdated Show resolved Hide resolved
@sycai sycai enabled auto-merge (squash) October 25, 2024 22:20
@sycai sycai disabled auto-merge October 25, 2024 22:21
@sycai sycai enabled auto-merge (squash) October 25, 2024 22:27
@sycai sycai merged commit 20e5c58 into main Oct 25, 2024
22 of 23 checks passed
@sycai sycai deleted the b364271921 branch October 25, 2024 23:19
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-dataframes API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants