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

Rename #sorbet_type#__tapioca_type #2125

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

amomchilov
Copy link
Contributor

@amomchilov amomchilov commented Dec 12, 2024

Motivation

Rename the #sorbet_type method called by the JsonApiClient::Resource compiler, to match our naming decision in #2112:

  1. Prefixed with __ to discourage the method from being called from regular user code (i.e. only Tapioca should be calling it).
  2. Renamed to emphasize Tapioca over Sorbet, to clarify how this ends up being used. Should hopefully be an easier term to search for, with fewer false matches.

Implementation

Print a warning when we find #sorbet_type, while still honouring it. If there's only a definition for #__tapioca_type, we'll use that instead.

After some migration time, we can drop support for #sorbet_type.

@amomchilov amomchilov added the breaking-change Non-backward compatible change label Dec 12, 2024
@amomchilov amomchilov self-assigned this Dec 12, 2024
@amomchilov amomchilov requested a review from a team as a code owner December 12, 2024 21:39
@amomchilov amomchilov added the enhancement New feature or request label Dec 12, 2024
@amomchilov amomchilov force-pushed the Alex/rename-sorbet_type-method branch from 37a0896 to 0f37d00 Compare December 12, 2024 21:47
@@ -147,7 +147,17 @@ def type_for(property)
return "T.untyped" if type.nil?

sorbet_type = if type.respond_to?(:sorbet_type)
line, file = type.method(:sorbet_type).source_location
Copy link
Contributor Author

@amomchilov amomchilov Dec 12, 2024

Choose a reason for hiding this comment

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

This assumes that the source_location is available (i.e. this was defined in Ruby, and not in a C extension). Is that safe to assume, or should I guard against it?

Doing so is a bit uglier:

msg = <<~MESSAGE
  WARNING: `#sorbet_type` is deprecated. Please rename your method to `#__tapioca_type`."
MESSAGE

if (source_location = type.method(:sorbet_type).source_location)
  msg.concat("Defined on line #{source_location[0]} of #{source_location[1]}")
end

msg = $stderr.puts

Copy link
Contributor

Choose a reason for hiding this comment

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

Could also be nil if there's some metaprogramming involved, so let's play it safe. You could extract out the deprecation stuff so as to not pollute the conditional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@amomchilov amomchilov force-pushed the Alex/rename-sorbet_type-method branch from 0f37d00 to 9298261 Compare December 13, 2024 20:22
@amomchilov amomchilov requested a review from andyw8 December 13, 2024 20:22
end
RUBY

rbi_for(:Post) # Should not raise
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have assert_nothing_raised, so a comment will suffice.

def self.method(name)
m = super
# Fake the source location, as if this was defined in a C extension.
def m.source_location
Copy link
Contributor Author

@amomchilov amomchilov Dec 13, 2024

Choose a reason for hiding this comment

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

It's surprisingly hard to make a method with no source_location ahha. Native methods from C extensions would have it, but other than that, every way to define a Ruby method ended up having it defined. I tried:

  1. define_singleton_method
  2. singleton_class.define_method
  3. eval
  4. class_eval
  5. Class.new { ... }

and they all had it defined.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IKR? I could have sworn you needed to explicitly pass in the ___FILE__ and __LINE__ else you got no info at all

@amomchilov amomchilov merged commit fcfc0ef into main Dec 16, 2024
28 checks passed
@amomchilov amomchilov deleted the Alex/rename-sorbet_type-method branch December 16, 2024 15:57
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 enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants