Shopify / Shopify/tapioca

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

Open
#1,165 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
873
Forks
164
Avg merge
4d 27m
Merged PRs (30d)
9

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the GenericValue, non_generic_value, generic_value, and explicit_generic_value examples, then trace Tapioca's handling of attribute's cast_type and generated DSL types. Done means the generated DSL resolves the type_member correctly for implicit and explicit generic casts instead of emitting an undefined ValueType or T.untyped.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.