-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add validation to check for non-ints from partitionfns #35881
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
Changes from 4 commits
200b753
549a1f4
f21e23e
22b8343
622bc06
ebcd037
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -162,6 +162,14 @@ def test_dofn_with_implicit_return_none_return_without_value(self): | |||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| class PartitionTest(unittest.TestCase): | ||||||||||||||||||
| def test_partition_with_bools(self): | ||||||||||||||||||
| with pytest.raises( | ||||||||||||||||||
| Exception, | ||||||||||||||||||
| match="PartitionFn yielded a 'bool' when it should only yields integers" | ||||||||||||||||||
| ): | ||||||||||||||||||
|
||||||||||||||||||
| with pytest.raises( | |
| Exception, | |
| match="PartitionFn yielded a 'bool' when it should only yields integers" | |
| ): | |
| with pytest.raises( | |
| ValueError, | |
| match="PartitionFn yielded a 'bool' when it should only yield integers" | |
| ): |
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.
Please fix this.
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 initially checked for ValueError and the test works with the directrunner but the portable runner will cast all exceptions to RuntimeError. (https://github.com/apache/beam/runs/48183396870)
I can narrow this to check for either RuntimeError or ValueError though
Outdated
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.
This test covers the case where the partition function returns a boolean. To make the tests more comprehensive, it would be beneficial to add another test case for other non-integer types that should be rejected, such as floats. This would ensure the validation logic is robust against various invalid types.
For example, you could add a test like this:
def test_partition_with_floats(self):
with pytest.raises(
ValueError,
match="PartitionFn yielded a 'float' when it should only yield integers"
):
with beam.testing.test_pipeline.TestPipeline() as p:
_ = (p | beam.Create([1.0]) | beam.Partition(lambda x, _: x, 2))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.
let us add another type like floats to test.
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.
added floats and a couple of other types
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.
The condition
isinstance(partition, bool) or not isinstance(partition, int)is correct, but it can be simplified. Sinceboolis a subclass ofint,isinstance(True, int)evaluates toTrue. A more direct way to check if a value is strictly an integer (and not a subclass likebool) is to usetype(partition) is not int. This is more readable and achieves the same result. I've also corrected a small grammatical error in the error message ('yields' to 'yield').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 thought about this. I am wondering whether we could do
partition = int(partition)although partition is bool, this could make the changes less aggressive.