Refactor generic patch to work properly with instance creation #268
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Our current generic patch was failing with
initialize
methods on generic types with signatures. The reason is that given a generic typeFoo
, itsinitialize
method would be wrapped in a Sorbet signature wrapper with an ultimatebind(self).call
to the original method in the wrapper.However, if we have something like
Foo[Bar].new
then, theself
in that wrapper would be an instance of a clone of theFoo
class, and will not be an instance of theFoo
class. Thus,self.is_a?(Foo)
would not be true in the wrapper, and thebind
call would fail with aTypeError
.Implementation
This was happening because we were cloning the constants that were given to us, which is fine for modules that don't have instances but fail like above for classes that will have instances. The fix is to create a subclass for classes instead of a clone, so that an instance of the class that represents
Foo[Bar]
will still be an instance ofFoo
.Tests
Expanded the tests to add the failure cases and verified that the
TypeError
was raised in the same way with the changed tests. Then implemented the fix to ensure that everything is still in order.