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

Improve address type hint #130

Merged
merged 5 commits into from
Nov 16, 2022
Merged

Improve address type hint #130

merged 5 commits into from
Nov 16, 2022

Conversation

daehan-koreapool
Copy link
Contributor

Goal:

  • Improve address.py module's type hint related code quality
$ make qa
poetry run flake8 pycardano
poetry run mypy --install-types --non-interactive pycardano
pycardano/address.py:154: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior  [str-bytes-safe]
pycardano/address.py:163: error: Argument 1 of "from_primitive" is incompatible with supertype "CBORSerializable"; supertype defines the argument type as "Union[bytes, bytearray, str, int, float, Decimal, bool, None, Tuple[Any, ...], List[Any], IndefiniteList, Dict[Any, Any], defaultdict[Any, Any], OrderedDict[Any, Any], Any, datetime, Pattern[Any], Any, Any, Set[Any], FrozenSet[Any]]"  [override]
pycardano/address.py:163: note: This violates the Liskov substitution principle
pycardano/address.py:163: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
pycardano/address.py:342: error: Argument 1 of "from_primitive" is incompatible with supertype "CBORSerializable"; supertype defines the argument type as "Union[bytes, bytearray, str, int, float, Decimal, bool, None, Tuple[Any, ...], List[Any], IndefiniteList, Dict[Any, Any], defaultdict[Any, Any], OrderedDict[Any, Any], Any, datetime, Pattern[Any], Any, Any, Set[Any], FrozenSet[Any]]"  [override]
pycardano/address.py:342: note: This violates the Liskov substitution principle
pycardano/address.py:342: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
pycardano/address.py:393: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior  [str-bytes-safe]
Found 4 errors in 1 file (checked 13 source files)

@daehan-koreapool
Copy link
Contributor Author

Updates

  • disabling str-bytes-safe mypy check since this only affects exception messages formatting bytes value in f-string in pycardano
  • from_primitive() now takes a generalized Primitive value and narrows down valid types within the function scope

@daehan-koreapool daehan-koreapool marked this pull request as ready for review November 14, 2022 09:05
@daehan-koreapool daehan-koreapool self-assigned this Nov 14, 2022
@daehan-koreapool daehan-koreapool added the enhancement New feature or request label Nov 14, 2022
@codecov-commenter
Copy link

Codecov Report

Merging #130 (f936287) into main (5340be1) will decrease coverage by 0.01%.
The diff coverage is 87.50%.

@@            Coverage Diff             @@
##             main     #130      +/-   ##
==========================================
- Coverage   85.99%   85.98%   -0.02%     
==========================================
  Files          25       25              
  Lines        2707     2711       +4     
  Branches      522      524       +2     
==========================================
+ Hits         2328     2331       +3     
  Misses        285      285              
- Partials       94       95       +1     
Impacted Files Coverage Δ
pycardano/nativescript.py 98.03% <ø> (ø)
pycardano/network.py 100.00% <ø> (ø)
pycardano/address.py 77.60% <85.71%> (-0.06%) ⬇️
pycardano/serialization.py 86.89% <100.00%> (ø)

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

pycardano/address.py Outdated Show resolved Hide resolved
test/pycardano/test_address.py Show resolved Hide resolved
UPDATE. an internal use only limit_primitive_type() helper decorator is added to reduce repeated code

UPDATE. specifying exact input type for child from_primitive()
@daehan-koreapool
Copy link
Contributor Author

I tried introducing limit_primitive_type() decorator class and removed all if not isinstance(value, type) repeated codes.
However, mypy wasn't smart enough to infer Primitive value type has been narrowed down and started raising bunch of Liskov errors again.

I think the cleanest solution is to declare from_primitive() base method to accept Any types of primitive values and just explicitly state allowed value types in overriding methods.
By doing so, both users and mypy understands what the expected input type is more clearly.

Also, I am keeping limit_primitive_type() decorator as internal only and not included in __all__ for now.

Copy link
Collaborator

@cffls cffls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limit_primitive_type looks very neat. Thanks for the change!

@daehan-koreapool daehan-koreapool merged commit c539d40 into main Nov 16, 2022
@daehan-koreapool daehan-koreapool deleted the improve-address-type-hint branch November 16, 2022 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants