You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by xq...@apache.org on 2025/09/02 14:53:25 UTC

(beam) branch master updated: Add validation to check for non-ints from partitionfns (#35881)

This is an automated email from the ASF dual-hosted git repository.

xqhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f3b160c100 Add validation to check for non-ints from partitionfns (#35881)
9f3b160c100 is described below

commit 9f3b160c100ec241b58f2bcfe361c02999bb12a2
Author: Hai Joey Tran <jo...@schrodinger.com>
AuthorDate: Tue Sep 2 10:53:18 2025 -0400

    Add validation to check for non-ints from partitionfns (#35881)
    
    * add validation to check for non-ints from partitions
    
    * move type check earlier
    
    * just use regular pipeline
    
    * just check for exception in case of portable runner
    
    * check for valueerror or runtimeerror
    
    * test a few more types
---
 sdks/python/apache_beam/transforms/core.py      |  4 ++++
 sdks/python/apache_beam/transforms/core_test.py | 14 ++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/sdks/python/apache_beam/transforms/core.py b/sdks/python/apache_beam/transforms/core.py
index 11c09702e9a..67f232b9ffa 100644
--- a/sdks/python/apache_beam/transforms/core.py
+++ b/sdks/python/apache_beam/transforms/core.py
@@ -3590,6 +3590,10 @@ class Partition(PTransformWithSideInputs):
     """A DoFn that applies a PartitionFn."""
     def process(self, element, partitionfn, n, *args, **kwargs):
       partition = partitionfn.partition_for(element, n, *args, **kwargs)
+      if isinstance(partition, bool) or not isinstance(partition, int):
+        raise ValueError(
+            f"PartitionFn yielded a '{type(partition).__name__}' "
+            "when it should only yield integers")
       if not 0 <= partition < n:
         raise ValueError(
             'PartitionFn specified out-of-bounds partition index: '
diff --git a/sdks/python/apache_beam/transforms/core_test.py b/sdks/python/apache_beam/transforms/core_test.py
index 57fa2151734..0d6be3fc7ae 100644
--- a/sdks/python/apache_beam/transforms/core_test.py
+++ b/sdks/python/apache_beam/transforms/core_test.py
@@ -163,6 +163,20 @@ class CreateTest(unittest.TestCase):
 
 
 class PartitionTest(unittest.TestCase):
+  def test_partition_with_bools(self):
+    with pytest.raises(
+        (ValueError, RuntimeError),
+        match=
+        r"PartitionFn yielded a '([^']*)' when it should only yield integers"):
+      # Check for RuntimeError too since the portable runner casts
+      # all exceptions to RuntimeError
+      invalid_inputs = [True, 1.2, 'string', None]
+      for input_value in invalid_inputs:
+        with beam.testing.test_pipeline.TestPipeline() as p:
+          _ = (
+              p | beam.Create([input_value])
+              | beam.Partition(lambda x, _: x, 2))
+
   def test_partition_boundedness(self):
     def partition_fn(val, num_partitions):
       return val % num_partitions