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

create_sig takes hash instead of TypedParams #1856

Merged
merged 1 commit into from
Apr 8, 2024

Conversation

bdewater
Copy link
Contributor

@bdewater bdewater commented Apr 2, 2024

Motivation

Follow-up to #1799. Based on this comment here: #1830 (comment) - TypeParam accepts a default, which is confusing since it's not used in sigs.

Implementation

Breaking change for custom compilers relying on the previous implementation, but it's a) not too hard to change with a regex-based find/replace and b) recent addition so probably not a lot usage yet.

Tests

Existing tests cover this.

@@ -126,15 +129,12 @@ def create_method_with_sigs(name, sigs:, parameters: [], class_method: false, vi

sig do
params(
parameters: T::Array[RBI::TypedParam],
parameters: T::Array[RBI::SigParam],
return_type: String,
).returns(RBI::Sig)
end
def create_sig(parameters: [], return_type: "T.untyped")
Copy link
Contributor Author

@bdewater bdewater Apr 2, 2024

Choose a reason for hiding this comment

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

Since this method now doesn't do anything of value, any objections to removing it?

Comment on lines 825 to 830
RBI::SigParam.new("start", "T.untyped"),
RBI::SigParam.new("finish", "T.untyped"),
RBI::SigParam.new("batch_size", "Integer"),
RBI::SigParam.new("error_on_ignore", "T.untyped"),
*(RBI::SigParam.new("order", "Symbol") if order),
RBI::SigParam.new("block", "T.proc.params(object: #{constant_name}).void"),
Copy link
Member

Choose a reason for hiding this comment

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

I suspect:

Suggested change
RBI::SigParam.new("start", "T.untyped"),
RBI::SigParam.new("finish", "T.untyped"),
RBI::SigParam.new("batch_size", "Integer"),
RBI::SigParam.new("error_on_ignore", "T.untyped"),
*(RBI::SigParam.new("order", "Symbol") if order),
RBI::SigParam.new("block", "T.proc.params(object: #{constant_name}).void"),
start: "T.untyped",
finish: "T.untyped",
batch_size: "Integer",
error_on_ignore: "T.untyped",
**({order: "Symbol"} if order),
block: "T.proc.params(object: #{constant_name}).void",

is a nicer and easier to read API, but it doesn't quite work due to **nil not being valid (until Ruby 3.4, at least).

Copy link
Member

Choose a reason for hiding this comment

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

Oh, this works:

Suggested change
RBI::SigParam.new("start", "T.untyped"),
RBI::SigParam.new("finish", "T.untyped"),
RBI::SigParam.new("batch_size", "Integer"),
RBI::SigParam.new("error_on_ignore", "T.untyped"),
*(RBI::SigParam.new("order", "Symbol") if order),
RBI::SigParam.new("block", "T.proc.params(object: #{constant_name}).void"),
start: "T.untyped",
finish: "T.untyped",
batch_size: "Integer",
error_on_ignore: "T.untyped",
**({order: "Symbol"} if order).to_h,
block: "T.proc.params(object: #{constant_name}).void",

even though the conditional part is a little ugly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the hash API and implemented that! Thanks for the suggestion.

The **().to_h trick is neat but the syntax is 😅. I ended up going with a simpler compact instead, but can change it if you strongly feel about using this style.

@andyw8 andyw8 removed their request for review April 2, 2024 20:09
TypeParam accepts a default, which is confusing since it's not used in
sigs. SigParam is the right thing to use here, and create_sig will
build these from a hash.
@bdewater bdewater force-pushed the overloaded-sig-clarification branch from 0bd71af to cdd80d1 Compare April 4, 2024 17:57
@paracycle
Copy link
Member

@bdewater thanks for the changes, I like what you've done with the hash.

In the meanwhile, @at and I paired to reimagine how adding params and signatures could be more fluent and came up with Shopify/rbi#284

@paracycle paracycle added the breaking-change Non-backward compatible change label Apr 4, 2024
@bdewater bdewater changed the title create_sig takes SigParams instead of TypedParams create_sig takes hash instead of TypedParams Apr 4, 2024
@KaanOzkan KaanOzkan merged commit cdd80d1 into Shopify:main Apr 8, 2024
18 of 20 checks passed
@bdewater bdewater deleted the overloaded-sig-clarification branch April 9, 2024 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Non-backward compatible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants