Fix: also suppress TypeError during type checking. #267
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.
Description & motivation
Currently an error is raised in below line, when an attempt is made to convert a given value to a float. In my case, this is a problem when I have a Union type hint that includes float, e.g.
Union[int, float, dict]
. During type checking, an iteration will be made over the types within the Union. Then, when the value is a dictionary, it will give a TypeError, which is currently not suppressed by thecontextlib.suppress()
call.specklepy/src/specklepy/objects/base.py
Line 312 in 420c73f
I agree that this Union type hint with both numeric types and a dictionary is not very common, but it still shouldn't give an error. By also suppressing TypeError, this is prevented.
Changes:
TypeError is also suppressed. This will suppress the error and of course evaluate to False for a check of a dict value against a float type hint. It will evaluate to True once the dict type hint is reached, i.e. the given dict value passes the check against the total Union type hint.
Validation of changes:
As said, in my case I had a Union type hint as
Union[float, dict]
. This gave an error when the given value was a dictionary. With this fix, that is prevented and the type check evaluates to True for a dictionary as input.@gjedlicska