-
Notifications
You must be signed in to change notification settings - Fork 42
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
Add annotations for shopify-money gem #153
Conversation
sig { params(numeric: Numeric).returns(T.noreturn) } | ||
def /(numeric); end |
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.
This method always raises an exception, hence T.noreturn
.
sig { params(num: Numeric).returns(T::Array[Money]) } | ||
def split(num); end | ||
|
||
sig { params(num: Numeric).returns(T::Hash[Money, Numeric]) } | ||
def calculate_splits(num); end |
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.
It doesn't really make sense to pass non-integers to the split
and calculate_splits
methods IMO, but technically the gem does support it (even though it produces surprising/arguably incorrect results with non-integers).
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.
Thanks this is great! But are we missing some important methods?
sig { returns(T.any(Money::Currency, Money::NullCurrency)) } | ||
attr_reader :currency |
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.
Annoyingly, Money::NullCurrency
is not a subclass of Money::Currency
, so we have to use a union.
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(T::Boolean) } | ||
def zero?(*args, **_arg1, &block); end | ||
|
||
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(T::Boolean) } | ||
def nonzero?(*args, **_arg1, &block); end | ||
|
||
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(T::Boolean) } | ||
def positive?(*args, **_arg1, &block); end | ||
|
||
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(T::Boolean) } | ||
def negative?(*args, **_arg1, &block); end | ||
|
||
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(Integer) } | ||
def to_i(*args, **_arg1, &block); end | ||
|
||
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(Float) } | ||
def to_f(*args, **_arg1, &block); end | ||
|
||
# @method_missing: delegated to BigDecimal | ||
sig { params(args: T.untyped, _arg1: T.untyped, block: T.nilable(T.proc.void)).returns(Integer) } | ||
def hash(*args, **_arg1, &block); end |
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.
The (*args, **_arg1, &block)
arguments are necessary to appease Sorbet, because that's the signature it generates for methods delegated via def_delegators
.
value: T.nilable(T.any(Money, Numeric, String)), | ||
currency: T.nilable(T.any(Money::Currency, Money::NullCurrency, String)), | ||
) | ||
.returns(Money) |
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.
This is now causing errors when Money
instances are multiplied with integers, 5 * Money.new
as the second argument isn't an Integer https://github.com/sorbet/sorbet/blob/dd4602e030832c930d0b413f31734a83708448f6/rbi/core/integer.rbi#L59.
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.
Ah, yeah, that's a bummer. I can't think of an easy fix though -- we'd need to be able to overload the signatures for Integer#*
, Float#*
, etc. with an additional signature, and I think Sorbet only supports overloading for its own stdlib types.
Maybe this is an acceptable limitation? It's easily fixed by swapping the arguments so that Money#*
is called instead. It would be nice if this could be made more obvious but I'm not sure how.
Open to any and all suggestions for improving this :)
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.
Yeah we can't represent the current implementation without overloading that signature.
I'm okay with swapping the arguments solution. Only problem is it's hard to communicate this moving forward.
Type of Change
Changes