tapioca icon indicating copy to clipboard operation
tapioca copied to clipboard

Invalid DSL when passing a class with a `type_parameter` to `attribute`'s `cast_type`

Open nicoco007 opened this issue 2 years ago • 2 comments

Sorry if this belongs on Discourse – I was initially going to post this there but I believe this is a bug considering invalid DSL is being generated rather than an error popping up with the code I've written. I can move to Discourse if it makes more sense there.

Tapioca generates invalid DSL when I pass an instance of a class that has a type_member to attribute's cast_type (2nd argument). For example, given a class GenericValue which inherits from ActiveRecord::Type::Value and that has a ValueType = type_member:

  • If I just do attribute :column, GenericValue.new, the methods in the generated DSL use just ValueType as a parameter/return value which doesn't exist outside GenericValue, so Sorbet raises Unable to resolve constant ValueType all over the place.
  • If I explicitly pass a type attribute :column, GenericValue[SomeObject].new, the methods in the generated DSL fall back to T.untyped, even if the parameter/return value in the serialize/deserialize do not use ValueType.

Here's an example commit: https://github.com/ShopifyFRS/bourgeois/compare/do-not-merge-generic-attribute-cast-type

  • non_generic_value uses ::Acceptance::SomeObject as expected.
  • generic_value uses ValueType instead of ::Acceptance::SomeObject and which is undefined outside Acceptance::GenericValue.
  • explicit_generic_value uses T.untyped instead of Acceptance::SomeObjectImpl (my expectation).

I've also tried reproducing the issue in a separate blank repo using the latest versions of Sorbet and Tapioca but the same thing happens. Unfortunately I cannot push this to GitHub since I cannot create repos in the Shopify organization.

nicoco007 avatar Sep 14 '22 21:09 nicoco007

You already made most of the investigation here. Could you write a test like this one in the test suite and maybe submit a PR fixing the issue?

rafaelfranca avatar Sep 14 '22 21:09 rafaelfranca

I'd be happy to give it a shot!

nicoco007 avatar Sep 15 '22 13:09 nicoco007