-
Notifications
You must be signed in to change notification settings - Fork 39
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 duration methods to Numeric #166
base: main
Are you sure you want to change the base?
Conversation
I expect it's not commonly used, but let's include And also |
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 think adding active_support/core_ext/numeric/time
to here will fix CI
Signed the CLA |
The failing methods don't exist on Numeric so they'd have to be removed. |
🤔 Seem they are instead defined here? https://github.com/rails/rails/blob/main/activesupport/lib/active_support/duration.rb |
That's the definition of I originally added all the definitions on Fixing it up now. |
I think I've got it right now. Thanks for your patience everyone. |
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'm conflicted about this PR because it'll cause a lot of typecheck errors due to the delegation happening in ActiveSupport::Duration
. I'm not sure if it'll bring in enough value. Applications are using Duration
in place of Numeric
.
For example, a common issue now would be while doing operations:
Time.now - 1.second
errors with Expected Time but found ActiveSupport::Duration for argument arg0
due to sorbet's internal RBIs.
ActiveSupport::Duration
is the correct type for 1.second
however it can be used in place of Numeric
and it's hard to tell Sorbet that. We'll have to be able to override the internal RBI. Looks like sorbet-typed
went this route before but I'm not sure how it was working for the above example.
Hm, those are good points.
I think this one is a bit less of an issue, as it effects expressions where a duration is the receiver (like
This is definitely annoying. It's also annoying to not be able to write
It looks like there were overrides: https://github.com/sorbet/sorbet-typed/blob/440aa126c55570ad35cb5a5d4fe4a392f23d65d3/lib/activesupport/all/activesupport.rbi#L1252-L1266 (again, very new to Sorbet here, and it seems like the story around overrides might have changed at some point) |
Yes but I'm unable to make them work in our monolith. The overrides aren't picked up by Sorbet. I will be discussing this with Stripe and see if Even with the overrides I'm not convinced that typing these as
Yeah currently the return type for |
Can we just hand-roll RBI for the methods we know are commonly being delegated? |
Not really. We can't add |
Any updates on this one? We've been getting some annoying behaviour when we have something like the following: one_day_ago = Time.now.utc - 1.day
# one_day_ago is inferred to be a `Float` |
I was able to make overloads for stdlib methods like |
See: #137
Type of Change
Changes
https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/numeric/time.rb#L9
https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/integer/time.rb