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

jrmccluskey 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 f01b9ddb05e Make Beartype use the default behavior in 
is_consistent_with() (#38275)
f01b9ddb05e is described below

commit f01b9ddb05eafa243e571c117a9a022b2f5e9b00
Author: Jack McCluskey <[email protected]>
AuthorDate: Mon May 11 13:51:37 2026 -0400

    Make Beartype use the default behavior in is_consistent_with() (#38275)
    
    * Make Beartype use the default behavior in is_consistent_with()
    
    * CHANGES.md callout
    
    * review comment
    
    * Update CHANGES.md
    
    Co-authored-by: gemini-code-assist[bot] 
<176961590+gemini-code-assist[bot]@users.noreply.github.com>
    
    * linting
    
    ---------
    
    Co-authored-by: gemini-code-assist[bot] 
<176961590+gemini-code-assist[bot]@users.noreply.github.com>
---
 CHANGES.md                                         |  1 +
 .../python/apache_beam/options/pipeline_options.py |  5 ++++
 sdks/python/apache_beam/typehints/typehints.py     | 12 +++++++++-
 .../python/apache_beam/typehints/typehints_test.py | 27 ++++++++++++++++++++++
 4 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/CHANGES.md b/CHANGES.md
index 0db7fddba4f..6deb653ffef 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -83,6 +83,7 @@
 
 ## Breaking Changes
 
+* (Python) Made Beartype the default fallback type checking tool. This can be 
disabled with the `--disable_beartype` pipeline option. 
([#38275](https://github.com/apache/beam/issues/38275))
 * X behavior was changed ([#X](https://github.com/apache/beam/issues/X)).
 
 ## Deprecations
diff --git a/sdks/python/apache_beam/options/pipeline_options.py 
b/sdks/python/apache_beam/options/pipeline_options.py
index 265313cd013..2533083f7e7 100644
--- a/sdks/python/apache_beam/options/pipeline_options.py
+++ b/sdks/python/apache_beam/options/pipeline_options.py
@@ -883,6 +883,11 @@ class TypeOptions(PipelineOptions):
         action='store_false',
         help='Disable type checking at pipeline construction '
         'time')
+    parser.add_argument(
+        '--disable_beartype',
+        default=False,
+        action='store_true',
+        help='Disable the use of beartype for type checking.')
     parser.add_argument(
         '--runtime_type_check',
         default=False,
diff --git a/sdks/python/apache_beam/typehints/typehints.py 
b/sdks/python/apache_beam/typehints/typehints.py
index eec9ea86bd4..6dc88a93dd3 100644
--- a/sdks/python/apache_beam/typehints/typehints.py
+++ b/sdks/python/apache_beam/typehints/typehints.py
@@ -1486,7 +1486,8 @@ _KNOWN_PRIMITIVE_TYPES.update({
 })
 
 
-def is_consistent_with(sub, base, use_beartype: bool = False) -> bool:
+def is_consistent_with(
+    sub, base, use_beartype: typing.Optional[bool] = None) -> bool:
   """Checks whether sub a is consistent with base.
 
   This is according to the terminology of PEP 483/484.  This relationship is
@@ -1495,6 +1496,15 @@ def is_consistent_with(sub, base, use_beartype: bool = 
False) -> bool:
   relation, but also handles the special Any type as well as type
   parameterization.
   """
+  if use_beartype is None:
+    from apache_beam.options.pipeline_options_context import 
get_pipeline_options
+    options = get_pipeline_options()
+    if options:
+      from apache_beam.options.pipeline_options import TypeOptions
+      use_beartype = not options.view_as(TypeOptions).disable_beartype
+    else:
+      use_beartype = True
+
   from apache_beam.pvalue import Row
   from apache_beam.typehints.row_type import RowTypeConstraint
   if sub == base:
diff --git a/sdks/python/apache_beam/typehints/typehints_test.py 
b/sdks/python/apache_beam/typehints/typehints_test.py
index a335ab05f1b..b097a01fed4 100644
--- a/sdks/python/apache_beam/typehints/typehints_test.py
+++ b/sdks/python/apache_beam/typehints/typehints_test.py
@@ -1614,6 +1614,33 @@ class DecoratorHelpers(TypeHintTestCase):
     self.assertTrue(is_consistent_with(int, pipe_union_2))
     self.assertTrue(is_consistent_with(float, pipe_union_2))
 
+  def test_is_consistent_with_disable_beartype(self):
+    import unittest.mock
+
+    from apache_beam.options.pipeline_options import PipelineOptions
+    from apache_beam.options.pipeline_options_context import 
scoped_pipeline_options
+
+    with unittest.mock.patch(
+        'apache_beam.typehints.typehints.is_subhint') as mock_is_subhint:
+      mock_is_subhint.return_value = True
+
+      class A:
+        pass
+
+      class B(A):
+        pass
+
+      options = PipelineOptions([])
+      with scoped_pipeline_options(options):
+        typehints.is_consistent_with(B, A)
+        self.assertTrue(mock_is_subhint.called)
+        mock_is_subhint.reset_mock()
+
+      options = PipelineOptions(['--disable_beartype'])
+      with scoped_pipeline_options(options):
+        typehints.is_consistent_with(B, A)
+        self.assertFalse(mock_is_subhint.called)
+
   def test_positional_arg_hints(self):
     self.assertEqual(typehints.Any, _positional_arg_hints('x', {}))
     self.assertEqual(int, _positional_arg_hints('x', {'x': int}))

Reply via email to