Skip to content
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[lang]: use keyword arguments for struct instantiation #3777

Merged
merged 23 commits into from
Feb 20, 2024

Conversation

tserg
Copy link
Collaborator

@tserg tserg commented Feb 13, 2024

What I did

closes: #2381

Change the instantiation of a struct from a dictionary to a callable with kwargs.

How I did it

Update the analysis and codegen, as well as grammar.

How to verify it

See tests

Commit message

feat: use keyword arguments for struct instantiation

This commit changes struct instantiation from taking a single dict
argument to taking keyword arguments.

For backwards compatibility (and to ease users into v0.4.0), the old
syntax is still supported, but a warning will be emitted.

Description for the changelog

Use kwargs for struct instantiation.

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov-commenter
Copy link

codecov-commenter commented Feb 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1fc819c) 84.99% compared to head (a777514) 85.00%.
Report is 2 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3777   +/-   ##
=======================================
  Coverage   84.99%   85.00%           
=======================================
  Files          92       92           
  Lines       13717    13731   +14     
  Branches     3079     3079           
=======================================
+ Hits        11659    11672   +13     
- Misses       1570     1572    +2     
+ Partials      488      487    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tserg tserg changed the title feat: use kwargs for struct ctor feat: use kwargs for struct instantiation Feb 14, 2024
@tserg tserg marked this pull request as ready for review February 14, 2024 09:55
Copy link
Member

@fubuloubu fubuloubu left a comment

Choose a reason for hiding this comment

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

love this change

@fubuloubu
Copy link
Member

btw isn't this tracked in a VIP somewhere? please link

@tserg
Copy link
Collaborator Author

tserg commented Feb 15, 2024

btw isn't this tracked in a VIP somewhere? please link

Yes, I have updated the top-level post with the link to the VIP.

@fubuloubu
Copy link
Member

btw isn't this tracked in a VIP somewhere? please link

Yes, I have updated the top-level post with the link to the VIP.

if you use closes: #X it will link and close the VIP as completed when this is merged

@tserg
Copy link
Collaborator Author

tserg commented Feb 16, 2024

btw isn't this tracked in a VIP somewhere? please link

Yes, I have updated the top-level post with the link to the VIP.

if you use closes: #X it will link and close the VIP as completed when this is merged

I was under the impression that this PR partially addressed the issue, as there was a separate discussion about having default values, but then I noticed your last comment. Thanks anyway!

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

this is looking good. we should add a warning if somebody tries to use the old way, instead of failing. i don't think we should break compatibility for at least a few releases, so people have time to ease into it.

i think we can keep support for the old way by adding an AST transformation, when Calls are constructed you can check if the argument is a dict, and transform it.

@charles-cooper charles-cooper changed the title feat: use kwargs for struct instantiation feat[lang]: use keyword arguments for struct instantiation Feb 19, 2024
@charles-cooper charles-cooper enabled auto-merge (squash) February 19, 2024 23:52
@charles-cooper charles-cooper merged commit 0752760 into vyperlang:master Feb 20, 2024
84 checks passed
@tserg tserg deleted the feat/change_struct_ctor branch February 20, 2024 03:17
@pcaversaccio
Copy link
Collaborator

I'm not sure whether this is intended behaviour or simply a regression tbh but if I use now the same variable name for the kwarg, it will fail:

image

struct Batch:
    target: address
    allow_failure: bool
    call_data: Bytes[max_value(uint16)]


struct Result:
    success: bool
    return_data: Bytes[max_value(uint8)]


@external
def multicall(data: DynArray[Batch, max_value(uint8)]) -> DynArray[Result, max_value(uint8)]:
    results: DynArray[Result, max_value(uint8)] = []
    return_data: Bytes[max_value(uint8)] = b""
    success: bool = empty(bool)
    for batch: Batch in data:
        if (batch.allow_failure == False):
            return_data = raw_call(batch.target, batch.call_data, max_outsize=255)
            success = True
            results.append(Result({success: success, return_data: return_data})) # This will pass
            results.append(Result({success=success, return_data=return_data})) # This will raise
        else:
            success, return_data = \
                raw_call(batch.target, batch.call_data, max_outsize=255, revert_on_failure=False)
            results.append(Result({success: success, return_data: return_data})) # This will pass
            results.append(Result({success=success, return_data=return_data})) # This will raise
    return results

Since the intention was to make it backward-compatible, using e.g. success=success should also compile IMO.

@tserg
Copy link
Collaborator Author

tserg commented Feb 20, 2024

results.append(Result({success=success, return_data=return_data})) # This will raise

The curly brackets are dropped in the new syntax, so this should compile:

results.append(Result(success=success, return_data=return_data))

@pcaversaccio
Copy link
Collaborator

results.append(Result({success=success, return_data=return_data})) # This will raise

The curly brackets are dropped in the new syntax, so this should compile:

results.append(Result(success=success, return_data=return_data))

Ah interesting, that indeed compiles, thx! Added this new feature to snekmate modules branch: pcaversaccio/snekmate@0c3a203. Vey nitpick: we should always follow PEP-8 where it states:

Don’t use spaces around the = sign when used to indicate a keyword argument

The two following tests don't follow this:

@pcaversaccio
Copy link
Collaborator

Also, I think it would make sense to update the docs here with the new syntax.

@tserg
Copy link
Collaborator Author

tserg commented Feb 20, 2024

Also, I think it would make sense to update the docs here with the new syntax.

Good point, I will follow up on this and the tests. Thanks!

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.

VIP: Convert Struct construction to use kwargs
5 participants