Skip to content

Validate field type on serializing #208

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

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

Attention: Patch coverage is 87.09677% with 4 lines in your changes missing coverage. Please review.

Project coverage is 85.60%. Comparing base (95881a0) to head (b3bccb6).
Report is 206 commits behind head on main.

Files with missing lines Patch % Lines
pycardano/serialization.py 86.66% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@           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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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