Skip to content

Conversation

@Rylan12
Copy link
Member

@Rylan12 Rylan12 commented Jan 9, 2026

Sorbet's default T::Struct::from_hash doesn't actually check to ensure that types match. Instead, let's override this method to use T::Struct::new which does perform type checking to catch additional issues.

I had to fix a few minor types, but otherwise all formulae and casks load successfully from the API, which I tested with Formula.all and Cask::Cask.all in brew irb.

To try to avoid more issues when loading old installed cask JSON data, I've added an exception to the cask handling: if a cask is loaded from JSON that came from the installed caskfile, it will stick with the old method that doesn't enforce types to avoid fatal errors there

Copilot AI review requested due to automatic review settings January 9, 2026 02:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request improves type safety by overriding T::Struct::from_hash in FormulaStruct and CaskStruct to use T::Struct::new, which performs runtime type checking. The PR also fixes several type definitions to ensure they accurately reflect the actual data structures used in the API.

Key changes:

  • Override from_hash methods to perform type checking via new instead of using Sorbet's default behavior
  • Correct type definitions for bottle checksums, dependency hashes, and cask dependencies
  • Add fallback for missing head URLs to satisfy type checker

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
Library/Homebrew/api/formula_struct.rb Adds from_hash override with type checking; defines DependencyHash type alias; corrects types for bottle_checksums, stable_dependency_hash, and head_dependency_hash
Library/Homebrew/api/formula.rb Adds empty string fallback for missing head URL to satisfy type checker
Library/Homebrew/api/cask_struct.rb Adds from_hash override with type checking; defines DependsOnArgs type alias to allow both single values and arrays; updates depends_on_args type

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Good idea, thanks for this!

@MikeMcQuaid MikeMcQuaid added this pull request to the merge queue Jan 9, 2026
Merged via the queue into main with commit b13eea0 Jan 9, 2026
37 checks passed
@MikeMcQuaid MikeMcQuaid deleted the string-type-checking-for-structs branch January 9, 2026 08:45
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.

3 participants