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

Add hash return type for relation groupchain size #2044

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/tapioca/dsl/compilers/active_record_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def create_group_chain_methods(klass)
return_type: "T.self_type",
)

CALCULATION_METHODS.each do |method_name|
(CALCULATION_METHODS + [:size]).each do |method_name|
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to not add :size into the CALCULATION_METHODS array itself?

Copy link
Contributor Author

@marknuzz marknuzz Oct 22, 2024

Choose a reason for hiding this comment

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

Hmm I'm actually not sure how to categorize this. These arrays are mostly organized based on the class that they were defined in, except the methods defined on ActiveRecord::Relation, where we have FIND_OR_CREATE_METHODS, BUILDER_METHODS, and TO_ARRAY_METHODS. But :size (like :to_a) is not something you can call directly on the model's class, so we can't have it in CommonRelationMethods or GeneratedRelationMethods. Unlike :to_a, :size's return type is different depending on context, so it didn't seem to fit into any of the above arrays.

But now that you bring it up, this seems like one of those cases where the method name doesn't fit nicely into into an array and the method should be defined outside of the scope of the case statement looped over an array.

And when looking into how the .to_a worked, I'm now finding that the GeneratedRelationMethods#where chain typing is actually incorrect. If you give an argument like User.where(foo: 1) you do not in fact get to use the methods on the PrivateAssociationRelationWhereChain returned. Additionally, you can't call :to_a on an object that is a where chain (User.where.not.to_a), unlike what the generated rbi suggests.

I think the best thing to do here might be to take a step back and make sure this structure is actually correct, and to figure out a way to automatically test this so that this can't happen. This PR might be a symptom of a larger problem and could complicate things further, and the .to_a additions were a reasonably good example of a case where this happened due to the assumption that the existing behavior was correct, when it wasn't. Should we close this for now or what do you think?

And now I can fully appreciate how difficult the job is of the Tapioca team!

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey Mark!

where the method name doesn't fit nicely into into an array and the method should be defined outside of the scope of the case statement looped over an array.

That seems reasonable to me.

If you give an argument like User.where(foo: 1) you do not in fact get to use the methods on the PrivateAssociationRelationWhereChain returned.

I can't test this right now, could you explain the issue?

And now I can fully appreciate how difficult the job is of the Tapioca team!

Haha thanks! Trying to tame Ruby meta-programming with types is a formidable challenge :)

Copy link
Member

Choose a reason for hiding this comment

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

I'd much rather see size explicitly handled separately, since it is not a calculation method. Moreover, the signature generated for size in this PR is not correct, since size does not take any parameters like column_name nor does it take a block.

Copy link
Contributor

@amomchilov amomchilov Nov 9, 2024

Choose a reason for hiding this comment

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

Regarding this:

And when looking into how the .to_a worked, I'm now finding that the GeneratedRelationMethods#where chain typing is actually incorrect. If you give an argument like User.where(foo: 1) you do not in fact get to use the methods on the PrivateAssociationRelationWhereChain returned. Additionally, you can't call :to_a on an object that is a where chain (User.where.not.to_a), unlike what the generated rbi suggests.

Ufuk has opened a PR to make "where chains" no longer be relations: #2070

case method_name
when :average, :maximum, :minimum
klass.create_method(
Expand Down Expand Up @@ -404,9 +404,9 @@ def create_group_chain_methods(klass)
],
return_type: "T::Hash[T.untyped, Integer]",
)
when :sum
when :sum, :size
klass.create_method(
"sum",
method_name.to_s,
parameters: [
create_opt_param("column_name", type: "T.nilable(T.any(String, Symbol))", default: "nil"),
create_block_param("block", type: "T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))"),
Expand Down
12 changes: 12 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,9 @@ def maximum(column_name); end
sig { params(column_name: T.any(String, Symbol)).returns(T::Hash[T.untyped, T.untyped]) }
def minimum(column_name); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def size(column_name = nil, &block); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def sum(column_name = nil, &block); end
end
Expand Down Expand Up @@ -758,6 +761,9 @@ def maximum(column_name); end
sig { params(column_name: T.any(String, Symbol)).returns(T::Hash[T.untyped, T.untyped]) }
def minimum(column_name); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def size(column_name = nil, &block); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def sum(column_name = nil, &block); end
end
Expand Down Expand Up @@ -1372,6 +1378,9 @@ def maximum(column_name); end
sig { params(column_name: T.any(String, Symbol)).returns(T::Hash[T.untyped, T.untyped]) }
def minimum(column_name); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def size(column_name = nil, &block); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def sum(column_name = nil, &block); end
end
Expand Down Expand Up @@ -1474,6 +1483,9 @@ def maximum(column_name); end
sig { params(column_name: T.any(String, Symbol)).returns(T::Hash[T.untyped, T.untyped]) }
def minimum(column_name); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def size(column_name = nil, &block); end

sig { params(column_name: T.nilable(T.any(String, Symbol)), block: T.nilable(T.proc.params(record: T.untyped).returns(T.untyped))).returns(T::Hash[T.untyped, T.any(Integer, Float, BigDecimal)]) }
def sum(column_name = nil, &block); end
end
Expand Down
Loading