Python: Improve runtime type checking error message #4333
Merged
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.
Fixes #4212
Overview
Error messages come from the beartype project. They provide much greater detail on typos or wrong value types when using the client.
Codegen was refactored a bit to accommodate:
NewType
which was only useful in static type checking.list
was changed toSequence
in field inputs1 to allow, for example tuples or other types of sequences, instead of only a list.Arg
was reverted to be simpler because we no longer need to pass context for the error message there.@typecheck
decorator was added to all methods that we want to do runtime type checking. It has all context for the origin of the error.Optional["DirectoryID"]
instead of"DirectoryID | None"
(notice quotes only around undefined type instead of the whole annotation).Traceback improvements
Before
After
Note: Values are actually colored to stand out.
Coroutines
We now detect if you forgot to add an
await
. Notice at the end: Did you forget to await?This tells you to:
Signed-off-by: Helder Correia
Footnotes
It's good practice to have broad inputs and narrow output in regards to type checking. ↩