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

Fix: also suppress TypeError during type checking. #267

Conversation

RobClaessensRHDHV
Copy link
Contributor

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 the contextlib.suppress() call.

return True, float(value)

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

This currently gives an error when float conversion is attempted with a non-numeric value.
@gjedlicska
Copy link
Collaborator

Hey @RobClaessensRHDHV

thanks for the fix, would u mind adding a simple test case for this scenario in https://github.com/specklesystems/specklepy/blob/main/tests/unit/test_type_validation.py#L109 ?

Also pls update your branch with latest main.
Thank you :)

@RobClaessensRHDHV
Copy link
Contributor Author

Hey @RobClaessensRHDHV

thanks for the fix, would u mind adding a simple test case for this scenario in https://github.com/specklesystems/specklepy/blob/main/tests/unit/test_type_validation.py#L109 ?

Also pls update your branch with latest main. Thank you :)

@gjedlicska
Done and done!

@gjedlicska
Copy link
Collaborator

Perfect 😊

@gjedlicska gjedlicska merged commit c7f5e07 into specklesystems:main Apr 6, 2023
@RobClaessensRHDHV
Copy link
Contributor Author

@gjedlicska Thanks for merging!

Will there be a quick patch including this fix?
We need it for an open pull request on our side.

@gjedlicska
Copy link
Collaborator

I just pushed one :)

@RobClaessensRHDHV RobClaessensRHDHV deleted the feature/suppress_type_error_during_type_check branch April 7, 2023 12:23
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.

2 participants