-
-
Notifications
You must be signed in to change notification settings - Fork 812
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[ux]: allow "compiling" .vyi
files
#4290
Merged
charles-cooper
merged 47 commits into
vyperlang:master
from
sandbubbles:feat/compile-solely-vyi
Dec 27, 2024
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
fb72306
set is_interface based on the suffix of an input file
sandbubbles 8bc61ed
construct ir_runtime only if input isn't interface
sandbubbles 112c76f
exclude some output formats when compiling interface
sandbubbles fc05efe
add test for interface outputs
sandbubbles e5fc9a7
iterate through all unsupported formats
sandbubbles 328f7fd
tag modules with is_interface
sandbubbles 36c620c
add is_interface tag to ModuleT
sandbubbles d181da1
add is_interface to parse_to_ast_with_settings
sandbubbles 5d4e55b
adjust tests
sandbubbles 6641092
lint
sandbubbles 87bc04e
merge master
sandbubbles 0908bdf
Merge branch 'master' into feat/compile-solely-vyi
sandbubbles afd8f8e
adjust solution due to merge
sandbubbles 8048d4f
reduce a multiline expression
charles-cooper 2651d89
fix mypy
charles-cooper 1134c3a
style: factor out an expression
charles-cooper b582f8f
Merge branch 'master' into feat/compile-solely-vyi
charles-cooper 5cdb2da
Merge branch 'master' into feat/compile-solely-vyi
charles-cooper 324b598
add flags into interface output
sandbubbles 50692a1
add test for presence of flags in interface output
sandbubbles fcfc2ec
lint
sandbubbles 60dddd4
capitalize the first letter but leave the rest the same
sandbubbles 77d62a5
Merge branch 'master' into feat/compile-solely-vyi
sandbubbles 3bf9389
fix flake8 errors
sandbubbles 6806134
Merge branch 'master' into feat/compile-solely-vyi
sandbubbles 5d954d8
remove duplicate list
sandbubbles 49385b7
Merge branch 'master' into feat/compile-solely-vyi
charles-cooper a21dc18
handle edgecases of filenames
sandbubbles af655be
add str function for FlagT
sandbubbles bef8725
test function with flag as return type
sandbubbles 59101c0
merge master
sandbubbles 4c476ad
take file_input from compiler_data after merge
sandbubbles 3bc7ef8
use the OUTPUT_FORMATS instead of listing all options
sandbubbles d851c73
replace capitilising by a cleaner solution
sandbubbles 92c883f
Merge branch 'master' into feat/compile-solely-vyi
sandbubbles a392898
remove unused list
sandbubbles e46ccb7
remove repeated code
charles-cooper bce2a08
test compilation round trip
sandbubbles e37f4f7
add comment
sandbubbles 53b4626
test weird interface name
sandbubbles cea1f21
add comment
sandbubbles d48b137
Merge branch 'master' into feat/compile-solely-vyi
sandbubbles be7318b
add parameter to inner function after merge with master
sandbubbles 5f04c0e
Merge branch 'master' into feat/compile-solely-vyi
charles-cooper 37666bc
explain test in comment
sandbubbles 574bee1
Merge branch 'master' into feat/compile-solely-vyi
sandbubbles 99a7605
Merge branch 'master' into feat/compile-solely-vyi
charles-cooper 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -876,3 +876,108 @@ def bar() -> uint256: | |
input_bundle = make_input_bundle({"lib1.vy": lib1}) | ||
c = get_contract(main, input_bundle=input_bundle) | ||
assert c.bar() == 1 | ||
|
||
|
||
def test_interface_with_flags(): | ||
code = """ | ||
struct MyStruct: | ||
a: address | ||
|
||
flag Foo: | ||
BOO | ||
MOO | ||
POO | ||
|
||
event Transfer: | ||
sender: indexed(address) | ||
|
||
@external | ||
def bar(): | ||
pass | ||
flag BAR: | ||
BIZ | ||
BAZ | ||
BOO | ||
|
||
@external | ||
@view | ||
def foo(s: MyStruct) -> MyStruct: | ||
return s | ||
""" | ||
|
||
out = compile_code(code, contract_path="code.vy", output_formats=["interface"])["interface"] | ||
|
||
assert "# Flags" in out | ||
assert "flag Foo:" in out | ||
assert "flag BAR" in out | ||
assert "BOO" in out | ||
assert "MOO" in out | ||
|
||
compile_code(out, contract_path="code.vyi", output_formats=["interface"]) | ||
|
||
|
||
vyi_filenames = [ | ||
"test__test.vyi", | ||
"test__t.vyi", | ||
"t__test.vyi", | ||
"t__t.vyi", | ||
"t_t.vyi", | ||
"test_test.vyi", | ||
"t_test.vyi", | ||
"test_t.vyi", | ||
"_test_t__t_tt_.vyi", | ||
"foo_bar_baz.vyi", | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("vyi_filename", vyi_filenames) | ||
def test_external_interface_names(vyi_filename): | ||
code = """ | ||
@external | ||
def foo(): | ||
... | ||
""" | ||
|
||
compile_code(code, contract_path=vyi_filename, output_formats=["external_interface"]) | ||
|
||
|
||
def test_external_interface_with_flag(): | ||
code = """ | ||
flag Foo: | ||
Blah | ||
|
||
@external | ||
def foo() -> Foo: | ||
... | ||
""" | ||
|
||
out = compile_code(code, contract_path="test__test.vyi", output_formats=["external_interface"])[ | ||
"external_interface" | ||
] | ||
assert "-> Foo:" in out | ||
|
||
|
||
def test_external_interface_compiles_again(): | ||
code = """ | ||
@external | ||
def foo() -> uint256: | ||
... | ||
@external | ||
def bar(a:int32) -> uint256: | ||
... | ||
""" | ||
|
||
out = compile_code(code, contract_path="test.vyi", output_formats=["external_interface"])[ | ||
"external_interface" | ||
] | ||
compile_code(out, contract_path="test.vyi", output_formats=["external_interface"]) | ||
|
||
|
||
@pytest.mark.xfail | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's please link: #4290 (comment) |
||
def test_weird_interface_name(): | ||
# based on comment https://github.com/vyperlang/vyper/pull/4290#discussion_r1884137428 | ||
# we replace "_" for "" which results in an interface without name | ||
out = compile_code("", contract_path="_.vyi", output_formats=["external_interface"])[ | ||
"external_interface" | ||
] | ||
assert "interface _:" in out |
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
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
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 |
---|---|---|
|
@@ -108,9 +108,8 @@ def build_integrity(compiler_data: CompilerData) -> str: | |
def build_external_interface_output(compiler_data: CompilerData) -> str: | ||
interface = compiler_data.annotated_vyper_module._metadata["type"].interface | ||
stem = PurePath(compiler_data.contract_path).stem | ||
# capitalize words separated by '_' | ||
# ex: test_interface.vy -> TestInterface | ||
name = "".join([x.capitalize() for x in stem.split("_")]) | ||
|
||
name = stem.title().replace("_", "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about _.vyi |
||
out = f"\n# External Interfaces\ninterface {name}:\n" | ||
|
||
for func in interface.functions.values(): | ||
|
@@ -136,6 +135,14 @@ def build_interface_output(compiler_data: CompilerData) -> str: | |
out += f" {member_name}: {member_type}\n" | ||
out += "\n\n" | ||
|
||
if len(interface.flags) > 0: | ||
out += "# Flags\n\n" | ||
for flag in interface.flags.values(): | ||
out += f"flag {flag.name}:\n" | ||
for flag_value in flag._flag_members: | ||
out += f" {flag_value}\n" | ||
out += "\n\n" | ||
charles-cooper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if len(interface.events) > 0: | ||
out += "# Events\n\n" | ||
for event in interface.events.values(): | ||
|
@@ -282,7 +289,8 @@ def build_method_identifiers_output(compiler_data: CompilerData) -> dict: | |
|
||
def build_abi_output(compiler_data: CompilerData) -> list: | ||
module_t = compiler_data.annotated_vyper_module._metadata["type"] | ||
_ = compiler_data.ir_runtime # ensure _ir_info is generated | ||
if not compiler_data.annotated_vyper_module.is_interface: | ||
_ = compiler_data.ir_runtime # ensure _ir_info is generated | ||
|
||
abi = module_t.interface.to_toplevel_abi_dict() | ||
if module_t.init_function: | ||
|
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
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.
maybe we should also check that the output compiles again (similar to a round trip)
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.
I've added that check for primitive types, for external interface it does not work with user defined ones.