-
Notifications
You must be signed in to change notification settings - Fork 167
refactor(fw,tests): fix address and hash types #422
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2c002ed
All changes
marioevz e0d9249
test fix
marioevz 3dfe2be
tests(fix): beacon root contract type
marioevz 6cae3ea
completely remove `to_address`, `to_hash`
marioevz e7d68b7
completely remove `to_hash_bytes` too
marioevz c088386
changelog
marioevz 3f1f987
Merge branch 'main' into fix-to-address
marioevz d8b7d8c
tox
marioevz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
""" | ||
Basic type primitives used to define other types. | ||
""" | ||
|
||
|
||
from typing import ClassVar, SupportsBytes, Type, TypeVar | ||
|
||
from .conversions import ( | ||
BytesConvertible, | ||
FixedSizeBytesConvertible, | ||
NumberConvertible, | ||
to_bytes, | ||
to_fixed_size_bytes, | ||
to_number, | ||
) | ||
from .json import JSONEncoder, SupportsJSON | ||
|
||
N = TypeVar("N", bound="Number") | ||
|
||
|
||
class Number(int, SupportsJSON): | ||
""" | ||
Class that helps represent numbers in tests. | ||
""" | ||
|
||
def __new__(cls, input: NumberConvertible | N): | ||
""" | ||
Creates a new Number object. | ||
""" | ||
return super(Number, cls).__new__(cls, to_number(input)) | ||
|
||
def __str__(self) -> str: | ||
""" | ||
Returns the string representation of the number. | ||
""" | ||
return str(int(self)) | ||
|
||
def __json__(self, encoder: JSONEncoder) -> str: | ||
""" | ||
Returns the JSON representation of the number. | ||
""" | ||
return str(self) | ||
|
||
def hex(self) -> str: | ||
""" | ||
Returns the hexadecimal representation of the number. | ||
""" | ||
return hex(self) | ||
|
||
@classmethod | ||
def or_none(cls: Type[N], input: N | NumberConvertible | None) -> N | None: | ||
""" | ||
Converts the input to a Number while accepting None. | ||
""" | ||
if input is None: | ||
return input | ||
return cls(input) | ||
|
||
|
||
class HexNumber(Number): | ||
""" | ||
Class that helps represent an hexadecimal numbers in tests. | ||
""" | ||
|
||
def __str__(self) -> str: | ||
""" | ||
Returns the string representation of the number. | ||
""" | ||
return self.hex() | ||
|
||
|
||
class ZeroPaddedHexNumber(HexNumber): | ||
""" | ||
Class that helps represent zero padded hexadecimal numbers in tests. | ||
""" | ||
|
||
def hex(self) -> str: | ||
""" | ||
Returns the hexadecimal representation of the number. | ||
""" | ||
if self == 0: | ||
return "0x00" | ||
hex_str = hex(self)[2:] | ||
if len(hex_str) % 2 == 1: | ||
return "0x0" + hex_str | ||
return "0x" + hex_str | ||
|
||
|
||
class Bytes(bytes, SupportsJSON): | ||
""" | ||
Class that helps represent bytes of variable length in tests. | ||
""" | ||
|
||
def __new__(cls, input: BytesConvertible): | ||
""" | ||
Creates a new Bytes object. | ||
""" | ||
return super(Bytes, cls).__new__(cls, to_bytes(input)) | ||
|
||
def __hash__(self) -> int: | ||
""" | ||
Returns the hash of the bytes. | ||
""" | ||
return super(Bytes, self).__hash__() | ||
|
||
def __str__(self) -> str: | ||
""" | ||
Returns the hexadecimal representation of the bytes. | ||
""" | ||
return self.hex() | ||
|
||
def __json__(self, encoder: JSONEncoder) -> str: | ||
""" | ||
Returns the JSON representation of the bytes. | ||
""" | ||
return str(self) | ||
|
||
def hex(self, *args, **kwargs) -> str: | ||
""" | ||
Returns the hexadecimal representation of the bytes. | ||
""" | ||
return "0x" + super().hex(*args, **kwargs) | ||
|
||
@classmethod | ||
def or_none(cls, input: "Bytes | BytesConvertible | None") -> "Bytes | None": | ||
""" | ||
Converts the input to a Bytes while accepting None. | ||
""" | ||
if input is None: | ||
return input | ||
return cls(input) | ||
|
||
|
||
T = TypeVar("T", bound="FixedSizeBytes") | ||
|
||
|
||
class FixedSizeBytes(Bytes): | ||
""" | ||
Class that helps represent bytes of fixed length in tests. | ||
""" | ||
|
||
byte_length: ClassVar[int] | ||
|
||
def __class_getitem__(cls, length: int) -> Type["FixedSizeBytes"]: | ||
""" | ||
Creates a new FixedSizeBytes class with the given length. | ||
""" | ||
|
||
class Sized(cls): # type: ignore | ||
byte_length = length | ||
|
||
return Sized | ||
|
||
def __new__(cls, input: FixedSizeBytesConvertible | T): | ||
""" | ||
Creates a new FixedSizeBytes object. | ||
""" | ||
return super(FixedSizeBytes, cls).__new__(cls, to_fixed_size_bytes(input, cls.byte_length)) | ||
|
||
def __hash__(self) -> int: | ||
""" | ||
Returns the hash of the bytes. | ||
""" | ||
return super(FixedSizeBytes, self).__hash__() | ||
|
||
@classmethod | ||
def or_none(cls: Type[T], input: T | FixedSizeBytesConvertible | None) -> T | None: | ||
""" | ||
Converts the input to a Fixed Size Bytes while accepting None. | ||
""" | ||
if input is None: | ||
return input | ||
return cls(input) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
""" | ||
Compares two FixedSizeBytes objects. | ||
""" | ||
if not isinstance(other, FixedSizeBytes): | ||
assert ( | ||
isinstance(other, str) | ||
or isinstance(other, int) | ||
or isinstance(other, bytes) | ||
or isinstance(other, SupportsBytes) | ||
) | ||
other = self.__class__(other) | ||
return super().__eq__(other) | ||
|
||
|
||
class Address(FixedSizeBytes[20]): # type: ignore | ||
""" | ||
Class that helps represent Ethereum addresses in tests. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class Hash(FixedSizeBytes[32]): # type: ignore | ||
""" | ||
Class that helps represent hashes in tests. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class Bloom(FixedSizeBytes[256]): # type: ignore | ||
""" | ||
Class that helps represent blooms in tests. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class HeaderNonce(FixedSizeBytes[8]): # type: ignore | ||
""" | ||
Class that helps represent the header nonce in tests. | ||
""" | ||
|
||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.