-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Update typecheck err msg #32880
Merged
Merged
Update typecheck err msg #32880
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
38ef421
Update typecheck err msg
hjtran 9973270
update a few typed_pipeline_test unit tests
hjtran afd0014
Move new logic to only apply to main element
hjtran d762186
fix tests
hjtran 934bd8c
remove please comment
hjtran f331aa3
Merge branch 'master' into typecheck_errmsg
hjtran 5f2529b
update tests again
hjtran 9ae70ce
remove debug str
hjtran 3ec5bda
fix more tests
hjtran 797b8eb
fix pubsub test
hjtran 0cee1f3
revert accidental pyproject change
hjtran e46b224
revert pyrpoject change
hjtran 28ab5c8
fix ptransform_test tests
hjtran 6ad169f
add explanatory comments for similar looking code
hjtran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1298,17 +1298,13 @@ class ToUpperCaseWithPrefix(beam.DoFn): | |
def process(self, element, prefix): | ||
return [prefix + element.upper()] | ||
|
||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Upper.*requires.*str.*applied.*int'): | ||
( | ||
self.p | ||
| 'T' >> beam.Create([1, 2, 3]).with_output_types(int) | ||
| 'Upper' >> beam.ParDo(ToUpperCaseWithPrefix(), 'hello')) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Upper': " | ||
"requires {} but got {} for element".format(str, int)) | ||
|
||
def test_do_fn_pipeline_runtime_type_check_satisfied(self): | ||
self.p._options.view_as(TypeOptions).runtime_type_check = True | ||
|
||
|
@@ -1335,18 +1331,14 @@ class AddWithNum(beam.DoFn): | |
def process(self, element, num): | ||
return [element + num] | ||
|
||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Add.*requires.*int.*applied.*str'): | ||
( | ||
self.p | ||
| 'T' >> beam.Create(['1', '2', '3']).with_output_types(str) | ||
| 'Add' >> beam.ParDo(AddWithNum(), 5)) | ||
self.p.run() | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Add': " | ||
"requires {} but got {} for element".format(int, str)) | ||
|
||
def test_pardo_does_not_type_check_using_type_hint_decorators(self): | ||
@with_input_types(a=int) | ||
@with_output_types(typing.List[str]) | ||
|
@@ -1355,17 +1347,13 @@ def int_to_str(a): | |
|
||
# The function above is expecting an int for its only parameter. However, it | ||
# will receive a str instead, which should result in a raised exception. | ||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'ToStr.*requires.*int.*applied.*str'): | ||
( | ||
self.p | ||
| 'S' >> beam.Create(['b', 'a', 'r']).with_output_types(str) | ||
| 'ToStr' >> beam.FlatMap(int_to_str)) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'ToStr': " | ||
"requires {} but got {} for a".format(int, str)) | ||
|
||
def test_pardo_properly_type_checks_using_type_hint_decorators(self): | ||
@with_input_types(a=str) | ||
@with_output_types(typing.List[str]) | ||
|
@@ -1387,7 +1375,8 @@ def to_all_upper_case(a): | |
def test_pardo_does_not_type_check_using_type_hint_methods(self): | ||
# The first ParDo outputs pcoll's of type int, however the second ParDo is | ||
# expecting pcoll's of type str instead. | ||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Upper.*requires.*str.*applied.*int'): | ||
( | ||
self.p | ||
| 'S' >> beam.Create(['t', 'e', 's', 't']).with_output_types(str) | ||
|
@@ -1398,11 +1387,6 @@ def test_pardo_does_not_type_check_using_type_hint_methods(self): | |
'Upper' >> beam.FlatMap(lambda x: [x.upper()]).with_input_types( | ||
str).with_output_types(str))) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Upper': " | ||
"requires {} but got {} for x".format(str, int)) | ||
|
||
def test_pardo_properly_type_checks_using_type_hint_methods(self): | ||
# Pipeline should be created successfully without an error | ||
d = ( | ||
|
@@ -1419,18 +1403,14 @@ def test_pardo_properly_type_checks_using_type_hint_methods(self): | |
def test_map_does_not_type_check_using_type_hints_methods(self): | ||
# The transform before 'Map' has indicated that it outputs PCollections with | ||
# int's, while Map is expecting one of str. | ||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Upper.*requires.*str.*applied.*int'): | ||
( | ||
self.p | ||
| 'S' >> beam.Create([1, 2, 3, 4]).with_output_types(int) | ||
| 'Upper' >> beam.Map(lambda x: x.upper()).with_input_types( | ||
str).with_output_types(str)) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Upper': " | ||
"requires {} but got {} for x".format(str, int)) | ||
|
||
def test_map_properly_type_checks_using_type_hints_methods(self): | ||
# No error should be raised if this type-checks properly. | ||
d = ( | ||
|
@@ -1449,17 +1429,13 @@ def upper(s): | |
|
||
# Hinted function above expects a str at pipeline construction. | ||
# However, 'Map' should detect that Create has hinted an int instead. | ||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Upper.*requires.*str.*applied.*int'): | ||
( | ||
self.p | ||
| 'S' >> beam.Create([1, 2, 3, 4]).with_output_types(int) | ||
| 'Upper' >> beam.Map(upper)) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Upper': " | ||
"requires {} but got {} for s".format(str, int)) | ||
|
||
def test_map_properly_type_checks_using_type_hints_decorator(self): | ||
@with_input_types(a=bool) | ||
@with_output_types(int) | ||
|
@@ -1477,7 +1453,8 @@ def bool_to_int(a): | |
def test_filter_does_not_type_check_using_type_hints_method(self): | ||
# Filter is expecting an int but instead looks to the 'left' and sees a str | ||
# incoming. | ||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Below 3.*requires.*int.*applied.*str'): | ||
( | ||
self.p | ||
| 'Strs' >> beam.Create(['1', '2', '3', '4', '5' | ||
|
@@ -1486,11 +1463,6 @@ def test_filter_does_not_type_check_using_type_hints_method(self): | |
str).with_output_types(str) | ||
| 'Below 3' >> beam.Filter(lambda x: x < 3).with_input_types(int)) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Below 3': " | ||
"requires {} but got {} for x".format(int, str)) | ||
|
||
def test_filter_type_checks_using_type_hints_method(self): | ||
# No error should be raised if this type-checks properly. | ||
d = ( | ||
|
@@ -1508,17 +1480,13 @@ def more_than_half(a): | |
return a > 0.50 | ||
|
||
# Func above was hinted to only take a float, yet a str will be passed. | ||
with self.assertRaises(typehints.TypeCheckError) as e: | ||
with self.assertRaisesRegex(typehints.TypeCheckError, | ||
r'Half.*requires.*float.*applied.*str'): | ||
( | ||
self.p | ||
| 'Ints' >> beam.Create(['1', '2', '3', '4']).with_output_types(str) | ||
| 'Half' >> beam.Filter(more_than_half)) | ||
|
||
self.assertStartswith( | ||
e.exception.args[0], | ||
"Type hint violation for 'Half': " | ||
"requires {} but got {} for a".format(float, str)) | ||
|
||
def test_filter_type_checks_using_type_hints_decorator(self): | ||
@with_input_types(b=int) | ||
def half(b): | ||
|
@@ -2128,14 +2096,10 @@ def test_mean_globally_pipeline_checking_violated(self): | |
self.p | ||
| 'C' >> beam.Create(['test']).with_output_types(str) | ||
| 'Mean' >> combine.Mean.Globally()) | ||
|
||
expected_msg = \ | ||
"Type hint violation for 'CombinePerKey': " \ | ||
"requires Tuple[TypeVariable[K], Union[<class 'float'>, <class 'int'>, " \ | ||
"<class 'numpy.float64'>, <class 'numpy.int64'>]] " \ | ||
"but got Tuple[None, <class 'str'>] for element" | ||
|
||
self.assertStartswith(e.exception.args[0], expected_msg) | ||
Comment on lines
-2131
to
-2138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and one of the other messages got too hard/frustrating to update to use with regex, so I just did some basic spot checking. The number of cases that the new asserts wouldn't catch but the old would I think are pretty few. |
||
err_msg = e.exception.args[0] | ||
assert "CombinePerKey" in err_msg | ||
assert "Tuple[TypeVariable[K]" in err_msg | ||
assert "Tuple[None, <class 'str'>" in err_msg | ||
|
||
def test_mean_globally_runtime_checking_satisfied(self): | ||
self.p._options.view_as(TypeOptions).runtime_type_check = True | ||
|
@@ -2195,14 +2159,12 @@ def test_mean_per_key_pipeline_checking_violated(self): | |
typing.Tuple[str, str])) | ||
| 'EvenMean' >> combine.Mean.PerKey()) | ||
self.p.run() | ||
|
||
expected_msg = \ | ||
"Type hint violation for 'CombinePerKey(MeanCombineFn)': " \ | ||
"requires Tuple[TypeVariable[K], Union[<class 'float'>, <class 'int'>, " \ | ||
"<class 'numpy.float64'>, <class 'numpy.int64'>]] " \ | ||
"but got Tuple[<class 'str'>, <class 'str'>] for element" | ||
|
||
self.assertStartswith(e.exception.args[0], expected_msg) | ||
err_msg = e.exception.args[0] | ||
assert "CombinePerKey(MeanCombineFn)" in err_msg | ||
assert "requires" in err_msg | ||
assert "Tuple[TypeVariable[K]" in err_msg | ||
assert "applied" in err_msg | ||
assert "Tuple[<class 'str'>, <class 'str'>]" in err_msg | ||
|
||
def test_mean_per_key_runtime_checking_satisfied(self): | ||
self.p._options.view_as(TypeOptions).runtime_type_check = True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a reason this type check is done up here rather than modifying the check nested in the for loop? It looks kind of redundant here.
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.
Yeah this is specifically a check for the main element while the loop is checking presumably side inputs.