Skip to content

Commit f01b9dd

Browse files
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>
1 parent f1bbb63 commit f01b9dd

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383

8484
## Breaking Changes
8585

86+
* (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))
8687
* X behavior was changed ([#X](https://github.com/apache/beam/issues/X)).
8788

8889
## Deprecations

sdks/python/apache_beam/options/pipeline_options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,11 @@ def _add_argparse_args(cls, parser):
883883
action='store_false',
884884
help='Disable type checking at pipeline construction '
885885
'time')
886+
parser.add_argument(
887+
'--disable_beartype',
888+
default=False,
889+
action='store_true',
890+
help='Disable the use of beartype for type checking.')
886891
parser.add_argument(
887892
'--runtime_type_check',
888893
default=False,

sdks/python/apache_beam/typehints/typehints.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,8 @@ def normalize(x, none_as_type=False):
14861486
})
14871487

14881488

1489-
def is_consistent_with(sub, base, use_beartype: bool = False) -> bool:
1489+
def is_consistent_with(
1490+
sub, base, use_beartype: typing.Optional[bool] = None) -> bool:
14901491
"""Checks whether sub a is consistent with base.
14911492
14921493
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:
14951496
relation, but also handles the special Any type as well as type
14961497
parameterization.
14971498
"""
1499+
if use_beartype is None:
1500+
from apache_beam.options.pipeline_options_context import get_pipeline_options
1501+
options = get_pipeline_options()
1502+
if options:
1503+
from apache_beam.options.pipeline_options import TypeOptions
1504+
use_beartype = not options.view_as(TypeOptions).disable_beartype
1505+
else:
1506+
use_beartype = True
1507+
14981508
from apache_beam.pvalue import Row
14991509
from apache_beam.typehints.row_type import RowTypeConstraint
15001510
if sub == base:

sdks/python/apache_beam/typehints/typehints_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,33 @@ def test_hint_helper_pipe_union(self):
16141614
self.assertTrue(is_consistent_with(int, pipe_union_2))
16151615
self.assertTrue(is_consistent_with(float, pipe_union_2))
16161616

1617+
def test_is_consistent_with_disable_beartype(self):
1618+
import unittest.mock
1619+
1620+
from apache_beam.options.pipeline_options import PipelineOptions
1621+
from apache_beam.options.pipeline_options_context import scoped_pipeline_options
1622+
1623+
with unittest.mock.patch(
1624+
'apache_beam.typehints.typehints.is_subhint') as mock_is_subhint:
1625+
mock_is_subhint.return_value = True
1626+
1627+
class A:
1628+
pass
1629+
1630+
class B(A):
1631+
pass
1632+
1633+
options = PipelineOptions([])
1634+
with scoped_pipeline_options(options):
1635+
typehints.is_consistent_with(B, A)
1636+
self.assertTrue(mock_is_subhint.called)
1637+
mock_is_subhint.reset_mock()
1638+
1639+
options = PipelineOptions(['--disable_beartype'])
1640+
with scoped_pipeline_options(options):
1641+
typehints.is_consistent_with(B, A)
1642+
self.assertFalse(mock_is_subhint.called)
1643+
16171644
def test_positional_arg_hints(self):
16181645
self.assertEqual(typehints.Any, _positional_arg_hints('x', {}))
16191646
self.assertEqual(int, _positional_arg_hints('x', {'x': int}))

0 commit comments

Comments
 (0)