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

Enable strict typing in CLI::Parser #17030

Merged
merged 4 commits into from Apr 21, 2024
Merged

Enable strict typing in CLI::Parser #17030

merged 4 commits into from Apr 21, 2024

Conversation

dduugg
Copy link
Sponsor Member

@dduugg dduugg commented Apr 4, 2024

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

I just wanted to capture #parser to work on Args namespacing, but decided to go all the way. (There's a pretty decent chance of type error creeping in, it's hard to find the right balance of rigor & narrowness without allowing an opportunity for some to slip in.)

@@ -26,6 +26,9 @@ def command_name = Utils.underscore(T.must(name).split("::").fetch(-1)).tr("_",
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) }
def command(name) = subclasses.find { _1.command_name == name }

sig { returns(T::Boolean) }
def dev_cmd? = T.must(name).start_with?("Homebrew::DevCmd")
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

This logic seems more appropriate to capture here

@@ -9,15 +9,28 @@
require "set"
require "utils/tty"

COMMAND_DESC_WIDTH = 80
OPTION_DESC_WIDTH = 45
HIDDEN_DESC_PLACEHOLDER = "@@HIDDEN@@"
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I moved these out of top level while I was here

@@ -119,49 +133,52 @@ def self.global_options
]
end

sig { params(option: String).returns(String) }
def self.option_to_name(option)
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I hoisted this with other class methods, to follow class layout conventions

Copy link
Contributor

Choose a reason for hiding this comment

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

There might be a cop for that.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Indeed, there is (though disabled by default)! https://docs.rubocop.org/rubocop/cops_layout.html#layoutclassstructure

@parser.summary_indent = " " * 2

@parser = T.let(OptionParser.new, OptionParser)
@parser.summary_indent = " "
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

It made more sense to just do the math on this one, IMO

else
# FIXME: remove once commands are all subclasses of `AbstractCommand`:
# Filter out Sorbet runtime type checking method calls.
cmd_location = T.must(caller_locations).select do |location|
cmd_location = caller_locations.select do |location|
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Not really sure why the T.must is no longer necessary, but \o/

@min_named_args = @max_named_args = number
elsif min.present? || max.present?
elsif min || max
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

removed the unecessary present?s while i was here

@@ -459,11 +479,7 @@ def hide_from_man_page!

private

SYMBOL_TO_USAGE_MAPPING = {
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

this isn't actually private, i hoisted it and added a private_constant declaration

@@ -590,12 +614,13 @@ def check_conflicts
end

select_cli_arg = violations.count - env_var_options.count == 1
raise OptionConflictError, violations.map(&method(:name_to_option)) unless select_cli_arg
raise OptionConflictError, violations.map { name_to_option(_1) } unless select_cli_arg
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

sorbet requires these to be removed under strict typing, to validate method arity

@@ -282,17 +294,23 @@ def name_to_option(name)
end
end

sig { params(names: String).returns(T.nilable(String)) }
def option_to_description(*names)
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

i think this logic is wrong (pretty sure it's choosing alphabetically, which seems off), but i left it alone

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. It's only used in the #option_description method as a fallback if an explicit description doesn't exist. It'd probably make more sense to grab the max by string length instead. I think all of our internal commands have descriptions so it doesn't really matter much for us. I'm fine with fixing it here or as a follow-up though.

@@ -7,6 +7,9 @@
#
# @api private
module Formatter
COMMAND_DESC_WIDTH = 80
OPTION_DESC_WIDTH = 45
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

These are only used in calls to Formatter, so seemed to make the most sense here

@dduugg dduugg force-pushed the strict-parser branch 3 times, most recently from 6a96f88 to 46549dd Compare April 4, 2024 20:51
Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Great work here @dduugg! A few nits but happy for them to be addressed in follow-up PRs or inline and then self-merged.

Library/Homebrew/brew.rb Outdated Show resolved Hide resolved
Library/Homebrew/brew.rb Outdated Show resolved Hide resolved
Library/Homebrew/cli/parser.rb Show resolved Hide resolved
Comment on lines +227 to 230
sig { params(text: T.nilable(String)).returns(T.nilable(String)) }
def description(text = nil)
return @description if text.blank?
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't need changed now but: this type of method API kinda sucks with Sorbet to me. Better to fall back to getters/setters.

Similarly, I wonder about returning empty strings more often rather than nilable strings.

Library/Homebrew/cli/parser.rb Outdated Show resolved Hide resolved
Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

@dduugg One final "add a Ruby comment" nit and I'm ✅ to ship!

Library/Homebrew/cli/parser.rb Show resolved Hide resolved
Library/Homebrew/cli/parser.rb Show resolved Hide resolved
@@ -119,49 +133,52 @@ def self.global_options
]
end

sig { params(option: String).returns(String) }
def self.option_to_name(option)
Copy link
Contributor

Choose a reason for hiding this comment

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

There might be a cop for that.

@@ -282,17 +294,23 @@ def name_to_option(name)
end
end

sig { params(names: String).returns(T.nilable(String)) }
def option_to_description(*names)
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. It's only used in the #option_description method as a fallback if an explicit description doesn't exist. It'd probably make more sense to grab the max by string length instead. I think all of our internal commands have descriptions so it doesn't really matter much for us. I'm fine with fixing it here or as a follow-up though.

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

@dduugg Good to self-merge when you are!

@dduugg dduugg enabled auto-merge April 21, 2024 21:35
@dduugg dduugg merged commit cfa3f92 into master Apr 21, 2024
30 checks passed
@dduugg dduugg deleted the strict-parser branch April 21, 2024 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants