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

Validate field type on serializing #208

Merged
merged 2 commits into from
Apr 16, 2023
Merged

Validate field type on serializing #208

merged 2 commits into from
Apr 16, 2023

Conversation

cffls
Copy link
Collaborator

@cffls cffls commented Apr 16, 2023

Validate each field in CBORSerializable during serialization. This will avoid bugs caused by incorrect type of field. For instance, when a field should be Address type, but being a string type instead, it would've been serialized successfully but failed to be deserialized.

This validation also helped discovering two minor type mismatch in unit tests. 🥳

Validate each field in CBORSerializable during serialization. This will avoid bugs caused by incorrect type of field.
For instance, when a field should be Address type, but being a string type instead, it would've been serialized successfully but failed to be deserialized.
@codecov-commenter
Copy link

codecov-commenter commented Apr 16, 2023

Codecov Report

Merging #208 (b3bccb6) into main (95881a0) will increase coverage by 0.00%.
The diff coverage is 87.09%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##             main     #208   +/-   ##
=======================================
  Coverage   85.59%   85.60%           
=======================================
  Files          26       26           
  Lines        2853     2882   +29     
  Branches      677      693   +16     
=======================================
+ Hits         2442     2467   +25     
- Misses        304      305    +1     
- Partials      107      110    +3     
Impacted Files Coverage Δ
pycardano/serialization.py 86.78% <86.66%> (-0.12%) ⬇️
pycardano/transaction.py 94.09% <100.00%> (+0.01%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@nielstron
Copy link
Contributor

Very neat! I like how this also applies to everything that is serialized very elegantly.

@nielstron
Copy link
Contributor

nielstron commented Apr 16, 2023

I just tried this with the error I initially reported. Its a better error message for sure, but there is still some improvement possible :)

    builder.required_signers = ["string!"]

results in

TypeError: Field 'required_signers' should be of type typing.Union[typing.List[pycardano.hash.VerificationKeyHash], 
NoneType], got <class 'list'> instead.

The error here is that I have a list of "str" and not list of the required type, which upsets the type checker - it fails to point out the precise violation though.

I think the only reasonable fix for this would be a recursive type printer. Will look into that.

@nielstron
Copy link
Contributor

nielstron commented Apr 16, 2023

Suggestion: Instead of printing the type of the violating object (which may hide recursive errors), just print the violating value? This also helps the developer debug, as they might have a better grip on where a value comes from than what type it has.

Changes the error message to

TypeError: Field 'required_signers' should be of type typing.Union[typing.List[pycardano.hash.VerificationKeyHash], 
NoneType], got ['string!'] instead.

Print violating field directly

Co-authored-by: Niels Mündler <[email protected]>
@cffls cffls merged commit 6ec8623 into Python-Cardano:main Apr 16, 2023
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.

3 participants