-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
feat: add conversion for fixed ABI types #1946
Merged
antazoey
merged 8 commits into
ApeWorX:main
from
BowTiedDevil:ape_fixed_decimal_conversion
Mar 7, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7092a63
Add `Decimal` as Python type for "fixed" ABI types
BowTiedDevil 1e1e0bd
Add converter for string-formatted floats to Decimal
BowTiedDevil 491e8d5
Add tests
BowTiedDevil 1034f6c
Fix newline formatting for black, update regex, add tests for bad inp…
BowTiedDevil ba341bb
Convert to raw string, use special characters (`\d`, `\.`) for digits…
BowTiedDevil b31222a
Add and rename tests, clarify `re.fullmatch` usage
BowTiedDevil 99f61b8
Use specific exception, add pattern match
BowTiedDevil 37ec1bd
Merge branch 'main' into ape_fixed_decimal_conversion
BowTiedDevil 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 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 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 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,83 @@ | ||
from decimal import Decimal | ||
|
||
import pytest | ||
|
||
from ape.exceptions import ConversionError | ||
|
||
|
||
def test_convert_formatted_float_strings_to_decimal(convert): | ||
test_strings = [ | ||
"1.000", | ||
"1.00", | ||
"1.0", | ||
"0.1", | ||
"0.01", | ||
"0.001", | ||
] | ||
for test_string in test_strings: | ||
actual = convert(test_string, Decimal) | ||
assert actual == Decimal(test_string) | ||
|
||
|
||
def test_convert_badly_formatted_float_strings_to_decimal(convert): | ||
test_strings = [ | ||
".1", | ||
"1.", | ||
".", | ||
] | ||
for test_string in test_strings: | ||
with pytest.raises( | ||
ConversionError, match=f"No conversion registered to handle '{test_string}'" | ||
): | ||
convert(test_string, Decimal) | ||
|
||
|
||
def test_convert_int_strings(convert): | ||
test_strings = [ | ||
"1", | ||
"10", | ||
"100", | ||
] | ||
for test_string in test_strings: | ||
with pytest.raises( | ||
ConversionError, match=f"No conversion registered to handle '{test_string}'" | ||
): | ||
convert(test_string, Decimal) | ||
|
||
|
||
def test_convert_alphanumeric_strings(convert): | ||
test_strings = [ | ||
"a", | ||
"H", | ||
"XYZ", | ||
] | ||
for test_string in test_strings: | ||
with pytest.raises( | ||
ConversionError, match=f"No conversion registered to handle '{test_string}'" | ||
): | ||
convert(test_string, Decimal) | ||
|
||
|
||
def test_convert_strings_with_token_names(convert): | ||
test_strings = [ | ||
"0.999 DAI", | ||
"10.0 USDC", | ||
] | ||
for test_string in test_strings: | ||
with pytest.raises( | ||
ConversionError, match=f"No conversion registered to handle '{test_string}'" | ||
): | ||
convert(test_string, Decimal) | ||
|
||
|
||
def test_convert_strings_with_ether_alias(convert): | ||
test_strings = [ | ||
"0 wei", | ||
"999 gwei", | ||
"1.0 ether", | ||
] | ||
for test_string in test_strings: | ||
with pytest.raises( | ||
ConversionError, match=f"No conversion registered to handle '{test_string}'" | ||
): | ||
convert(test_string, Decimal) |
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.
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.
re.fullmatch
is the same as checking the full string? That's helpful because we do already have conversion sequences like"1.0 ether"
and"10.0 USDC"
Can you add a comment to this effect?
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.
Confirmed,
re.fullmatch
will check the full string - it should be equivalent to sandwiching the above expression with^...$
but I'm not experienced enough with regex to know for sure if that expression has edge cases.Added a note to the comment with b31222a