Fix generic ActiveJob signatures - #2696
Conversation
|
I have signed the CLA! |
| #: (String constant_name) -> String | ||
| def generic_job_type(constant_name) |
There was a problem hiding this comment.
Let's document this
| #: (String constant_name) -> String | |
| def generic_job_type(constant_name) | |
| # Resolves the constant name into a valid Sorbet type reference, | |
| # applying `T.untyped` for any unfixed generic type variables. | |
| # | |
| # @example | |
| # generic_job_type("StandardJob") # => "StandardJob" | |
| # generic_job_type("GenericJob") # => "GenericJob[T.untyped]" | |
| #: (String constant_name) -> String | |
| def generic_job_type(constant_name) |
There was a problem hiding this comment.
Thanks! I've added documentation for generic_job_type in 85766fa, including examples for standard and generic jobs. The focused ActiveJob spec, RuboCop, docs generation, and typecheck all pass.
There was a problem hiding this comment.
Hey @iamhaseebn, that doc got lost in one of your subsequent commits.
Can you add it back, and tidy up the git history? I think most (all?) of this makes sense to squash down to just one commit.
There was a problem hiding this comment.
Good catch, thanks - I've restored the documentation and squashed this down to one signed commit.
77897fd to
e1504c6
Compare
paracycle
left a comment
There was a problem hiding this comment.
Sorry, I realize type_name_of is a better name for the method, since it returns the proper Sorbet type name, instead of the constant name. Also, it is much safer to make it call qualified_name_of so that the names are anchored properly.
I think after this, I am good to merge. Thank you!
| private | ||
|
|
||
| #: (Array[RBI::TypedParam] parameters, String? constant_name) -> Array[RBI::TypedParam] | ||
| # Resolves a constant name into a valid Sorbet type reference, |
There was a problem hiding this comment.
| # Resolves a constant name into a valid Sorbet type reference, | |
| # Resolves a constant into a valid Sorbet type reference, |
| # generic_name_of(StandardJob) # => "StandardJob" | ||
| # generic_name_of(GenericJob) # => "GenericJob[T.untyped]" | ||
| #: (Module[top] constant) -> String? | ||
| def generic_name_of(constant) |
There was a problem hiding this comment.
| # generic_name_of(StandardJob) # => "StandardJob" | |
| # generic_name_of(GenericJob) # => "GenericJob[T.untyped]" | |
| #: (Module[top] constant) -> String? | |
| def generic_name_of(constant) | |
| # type_name_of(StandardJob) # => "::StandardJob" | |
| # type_name_of(GenericJob) # => "::SomeModule::GenericJob[T.untyped]" | |
| #: (Module[top] constant) -> String? | |
| def type_name_of(constant) |
| # generic_name_of(GenericJob) # => "GenericJob[T.untyped]" | ||
| #: (Module[top] constant) -> String? | ||
| def generic_name_of(constant) | ||
| type_name = name_of(constant) |
There was a problem hiding this comment.
| type_name = name_of(constant) | |
| type_name = qualified_name_of(constant) |
| assert_equal(expected, rbi_for(:NotifyJob)) | ||
| end | ||
|
|
||
| it "generates correct RBI file for a generic job" do |
There was a problem hiding this comment.
I don't think this test adds much value given the test below exists. Can we just name the test below "generates correct RBI file for a generic job" instead and remove this?
| Input = type_member | ||
| Output = type_member |
There was a problem hiding this comment.
Testing with multiple kinds of type members would be great:
| Input = type_member | |
| Output = type_member | |
| Input = type_member | |
| Fixed = type_member { { fixed: String } } | |
| Output = type_member { { upper: ActiveRecord::Base } } |
e1504c6 to
4b9ca6e
Compare
|
Thanks! I've applied all five suggestions and pushed the update. |
Motivation
Generic ActiveJob subclasses currently generate bare job class references in
perform_latersignatures. Sorbet requires generic classes to include type arguments, so the generated RBI fails type checking.Fixes #2233.
Implementation
T::Generic === constant.T.untypedfor each generated type argument.perform_laterreturn type.Tests
bundle exec bin/test spec/tapioca/dsl/compilers/active_job_spec.rb.bundle exec bin/typecheckandbundle exec bin/style.