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

Enabling typing in Homebrew::API module #14623

Merged
merged 1 commit into from Feb 14, 2023
Merged

Conversation

dduugg
Copy link
Sponsor Member

@dduugg dduugg commented Feb 14, 2023

  • 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?

This enables typing for all the files that I could find under Homebrew::API

@@ -15,12 +15,10 @@ module API

extend Cachable

module_function
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 admittedly don't like module_fuction, but I also don't think the functions below actually work as private instance methods, they rely on extend Cachable, which only makes methods available at the class level, iiuc.

HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze

sig { params(endpoint: String).returns(Hash) }
def fetch(endpoint)
def self.fetch(endpoint)
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.

Note that this is a style deviation from the files under Library/Homebrew/api/, which use the class << self syntax to define class methods. I prefer the style here, as it makes it obvious at the def where the method is being defined, but I'm equally happy to match the preferred style.

@@ -37,8 +35,8 @@ def fetch(endpoint)
raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
end

sig { params(endpoint: String, target: Pathname).returns(Hash) }
def fetch_json_api_file(endpoint, target:)
sig { params(endpoint: String, target: Pathname).returns(T.any(Array, Hash)) }
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 usually use T.untyped for methods that return parsed JSON (as does sorbet itself), but was able to get tests to pass with a slight extension of the existing sig here.

@@ -61,7 +59,7 @@ def fetch_json_api_file(endpoint, target:)
begin
begin
args = curl_args.dup
args.prepend("--time-cond", target) if target.exist? && !target.empty?
args.prepend("--time-cond", target.to_s) if target.exist? && !target.empty?
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 believe it is safe to explicitly stringify the Pathname object here, but another option would be to define args as a mixed Array of String and Pathname objects.

@dduugg dduugg force-pushed the type-api branch 2 times, most recently from dc8b473 to f6f3d09 Compare February 14, 2023 05:48
@dduugg dduugg marked this pull request as ready for review February 14, 2023 05:48
@Bo98
Copy link
Member

Bo98 commented Feb 14, 2023

The rest of this LGTM

@MikeMcQuaid MikeMcQuaid merged commit 72071f2 into Homebrew:master Feb 14, 2023
@MikeMcQuaid
Copy link
Member

Thanks again @dduugg!

@dduugg dduugg deleted the type-api branch February 14, 2023 16:39
@github-actions github-actions bot added the outdated PR was locked due to age label Mar 18, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants