Skip to content
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

Better error messages for Type Errors #655

Merged
merged 11 commits into from
Sep 25, 2021
Merged

Better error messages for Type Errors #655

merged 11 commits into from
Sep 25, 2021

Conversation

kumare3
Copy link
Contributor

@kumare3 kumare3 commented Sep 13, 2021

TL;DR

This PR intends to improve the error messages in case of type errors and also introduces stricter type checking for primitive types.

Type

  • Bug Fix
  • Feature
  • Plugin

Are all requirements met?

  • Code completed
  • Smoke tested
  • Unit tests added
  • Code documentation added
  • Any pending items have an associated Issue

Complete description

How did you fix the bug, make the feature etc. Link to any design docs etc

Tracking Issue

fixes flyteorg/flyte#1439
fixes flyteorg/flyte#1423

Follow-up issue

NA

kumare3 and others added 4 commits September 15, 2021 15:53
Signed-off-by: Ketan Umare <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
@codecov
Copy link

codecov bot commented Sep 15, 2021

Codecov Report

Merging #655 (744ef4c) into master (373e50b) will increase coverage by 0.03%.
The diff coverage is 89.09%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #655      +/-   ##
==========================================
+ Coverage   85.57%   85.61%   +0.03%     
==========================================
  Files         355      355              
  Lines       29341    29549     +208     
  Branches     2379     2405      +26     
==========================================
+ Hits        25110    25298     +188     
- Misses       3594     3610      +16     
- Partials      637      641       +4     
Impacted Files Coverage Δ
flytekit/types/schema/types.py 77.35% <28.57%> (-0.63%) ⬇️
flytekit/core/base_task.py 89.04% <66.66%> (ø)
flytekit/core/condition.py 89.30% <76.92%> (-0.70%) ⬇️
tests/flytekit/unit/core/test_type_hints.py 95.70% <91.30%> (-0.16%) ⬇️
flytekit/core/promise.py 77.70% <94.11%> (+1.13%) ⬆️
flytekit/core/type_engine.py 87.46% <100.00%> (+0.74%) ⬆️
flytekit/types/directory/types.py 90.16% <100.00%> (+0.33%) ⬆️
flytekit/types/file/file.py 88.02% <100.00%> (-1.19%) ⬇️
tests/flytekit/unit/common_tests/test_promise.py 100.00% <100.00%> (ø)
tests/flytekit/unit/core/test_conditions.py 98.70% <100.00%> (ø)
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 373e50b...744ef4c. Read the comment docs.

Signed-off-by: Kevin Su <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
Signed-off-by: Kevin Su <[email protected]>
@pingsutw pingsutw changed the title [wip] Better error messages for Type Errors Better error messages for Type Errors Sep 16, 2021
@kumare3 kumare3 requested a review from eapolinario September 17, 2021 05:42
Copy link
Contributor

@wild-endeavor wild-endeavor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test one of these errors on console? just want to see what it looks like

logger.error(f"failed to convert return value for var {k} with error {type(e)}: {e}")
raise e
logger.error(f"Failed to convert return value for var {k} with error {type(e)}: {e}")
raise TypeError(f"Failed to convert return value for var {k} for function {self.name}") from e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we seen how this looks on console? i remember the last time I tried the from e syntax, a lot of the stack trace was missing and it became a lot harder to debug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traceback (most recent call last):
  File "/Users/kevin/git/flytekit/flytekit/core/base_task.py", line 508, in dispatch_execute
    literals[k] = TypeEngine.to_literal(exec_ctx, v, py_type, literal_type)
  File "/Users/kevin/git/flytekit/flytekit/core/type_engine.py", line 390, in to_literal
    transformer.assert_type(python_type, python_val)
  File "/Users/kevin/git/flytekit/flytekit/core/type_engine.py", line 65, in assert_type
    raise TypeError(f"Type of Val '{v}' is not an instance of {t}")
TypeError: Type of Val 'Welcome, kevin!' is not an instance of <class 'int'>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    print("Final result", welcome(name="kevin"))
  File "/Users/kevin/git/flytekit/flytekit/core/workflow.py", line 236, in __call__
    return flyte_entity_call_handler(self, *args, **input_kwargs)
  File "/Users/kevin/git/flytekit/flytekit/core/promise.py", line 876, in flyte_entity_call_handler
    result = cast(LocallyExecutable, entity).local_execute(child_ctx, **kwargs)
  File "/Users/kevin/git/flytekit/flytekit/core/workflow.py", line 251, in local_execute
    function_outputs = self.execute(**kwargs)
  File "/Users/kevin/git/flytekit/flytekit/core/workflow.py", line 686, in execute
    return exception_scopes.user_entry_point(self._workflow_function)(**kwargs)
  File "/Users/kevin/git/flytekit/flytekit/common/exceptions/scopes.py", line 198, in user_entry_point
    return wrapped(*args, **kwargs)
  File "test.py", line 22, in welcome
    greeting = greet(name=name)
  File "/Users/kevin/git/flytekit/flytekit/core/base_task.py", line 274, in __call__
    return flyte_entity_call_handler(self, *args, **kwargs)
  File "/Users/kevin/git/flytekit/flytekit/core/promise.py", line 869, in flyte_entity_call_handler
    return cast(LocallyExecutable, entity).local_execute(ctx, **kwargs)
  File "/Users/kevin/git/flytekit/flytekit/core/base_task.py", line 255, in local_execute
    outputs_literal_map = self.dispatch_execute(ctx, input_literal_map)
  File "/Users/kevin/git/flytekit/flytekit/core/base_task.py", line 511, in dispatch_execute
    raise TypeError(f"Failed to convert return value for var {k} for function {self.name}") from e
TypeError: Failed to convert return value for var o0 for function __main__.greet

Copy link
Member

@pingsutw pingsutw Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pingsutw
pingsutw previously approved these changes Sep 23, 2021
Copy link
Collaborator

@eapolinario eapolinario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is major step up in usability. Thanks a lot for working on it!

def foo2(a: int, b: str) -> typing.Tuple[int, str]:
return "hello", 10

with pytest.raises(TypeError):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also use the match parameter for this test? Just to confirm that the message is what we expect.

@@ -1338,3 +1340,16 @@ def t2() -> dict:
expected_struct = Struct()
expected_struct.update({"k1": "v1", "k2": 3, "4": {"one": [1, "two", [3]]}})
assert output_lm.literals["o0"].scalar.generic == expected_struct


def test_error_messages():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have a test where the mismatch happens in a proto? We had an issue in the OSS slack where a workflow input of type Dictionary but it was being passed a list of dictionaries (and as you can see from the slack message, the error is not intuitive).

Signed-off-by: Kevin Su <[email protected]>
@pingsutw pingsutw merged commit 2fcb014 into master Sep 25, 2021
@cosmicBboy cosmicBboy mentioned this pull request Oct 6, 2021
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Type is not enforced at compile-time for Primitive types [BUG] Non-descriptive error messages
4 participants