Skip to content

Improves typing in Hash classes #147

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
Mar 9, 2021
Merged

Improves typing in Hash classes #147

merged 2 commits into from
Mar 9, 2021

Conversation

tedkalaw
Copy link
Contributor

@tedkalaw tedkalaw commented Mar 9, 2021

High Level Overview of Change

TSIA

Context of Change

Needed for mypy use

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release
  • Typing

Before / After

Hash classes pass mypy --strict checks

Test Plan

CI
poetry run mypy --strict xrpl/binarycodec
No errors should originate from xrpl/binarycodec

Comment on lines +79 to +80
def _get_length(cls: Type[Hash]) -> int:
raise NotImplementedError("Hash._get_length not implemented")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

a better way to do this would probably use some combination of @Property, @AbstractMethod, and @classmethod, so that the base class implements an abstract getter for a class property and the subclasses can just use the length = 20 syntax

unfortunately, this isn't officially supported by mypy: python/mypy#8532
as such, i went with using a non-property getter. let me know what you think

"""
if len(buffer) != self._LENGTH:
buffer = buffer if buffer is not None else bytes(self._get_length())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

without this, Hash.init struggles to reason about the type of buffer, even though the subclasses have this check. it seemed easier to move this here

@tedkalaw tedkalaw marked this pull request as ready for review March 9, 2021 22:12
@tedkalaw tedkalaw merged commit 87a7853 into master Mar 9, 2021
@tedkalaw tedkalaw deleted the hash-types-duo branch March 9, 2021 22:59
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