-
Notifications
You must be signed in to change notification settings - Fork 106
Improves typing in Hash classes #146
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
Conversation
xrpl/binarycodec/types/hash.py
Outdated
""" | ||
Construct a Hash. | ||
|
||
:param buffer: The byte buffer that will be used to store | ||
the serialized encoding of this field. | ||
""" | ||
buffer = buffer if buffer is not None else bytes(self._LENGTH) |
There was a problem hiding this comment.
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
def _get_length(cls: Type[Hash]) -> int: | ||
raise NotImplementedError("Hash._get_length not implemented") |
There was a problem hiding this comment.
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
High Level Overview of Change
TSIA
Context of Change
Needed for mypy use
Type of Change
Before / After
Hash classes pass mypy --strict checks
Test Plan
CI
poetry run mypy --strict xrpl/binarycodec
No errors should originate from
xrpl/binarycodec