Skip to content

Commit

Permalink
Preempt issubclass error for generic types
Browse files Browse the repository at this point in the history
Previously, parameterized generics that made it to the issubclass call would
fail and cause obscure stack traces to bubble up. Short-circuiting this logic
by unconditionally rejecting ensures the type check failure case will handle
the error message generation and display.
  • Loading branch information
msuozzo committed Feb 15, 2025
1 parent 2317228 commit 00370c0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sdks/python/apache_beam/typehints/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,10 @@ def is_consistent_with(sub, base):
elif isinstance(sub, TypeConstraint):
# Nothing but object lives above any type constraints.
return base == object
elif is_typing_generic(base):
# Cannot check unsupported parameterized generic which will cause issubclass
# to fail with an exception.
return False
return issubclass(sub, base)


Expand Down
1 change: 1 addition & 0 deletions sdks/python/apache_beam/typehints/typehints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,7 @@ def test_hint_helper(self):
self.assertFalse(is_consistent_with(object, str))
self.assertTrue(is_consistent_with(str, Union[str, int]))
self.assertFalse(is_consistent_with(Union[str, int], str))
self.assertFalse(is_consistent_with(str, NonBuiltInGeneric[str]))

def test_positional_arg_hints(self):
self.assertEqual(typehints.Any, _positional_arg_hints('x', {}))
Expand Down

0 comments on commit 00370c0

Please sign in to comment.