Skip to content

Replace untyped return types in PP with concrete types - #3234

Merged
ksss merged 1 commit into
ruby:masterfrom
apiology:type-pp-return-values
Oct 15, 2026
Merged

ksss merged 1 commit into
ruby:masterfrom
apiology:type-pp-return-values

Conversation

@apiology

Copy link
Copy Markdown
Contributor

Claude: This PR was written by Claude Code on behalf of @apiology.

Problem:

Most return types in stdlib/pp/0/pp.rbs are untyped, so values from PP lose their types:

s = PP.pp(obj, +"")      # untyped; PP.pp returns `out`, a String here
Foo.new.inspect          # untyped when `alias inspect pretty_print_inspect`; it returns a String

Solution:

Following lib/pp.rb (v0.6.4):

Ruby Method untyped → Basis
PP::_PrettyPrint#pretty_print, #pretty_print_cycle void called for their effect on the printer
PP::ObjectMixin#pretty_print, #pretty_print_cycle void same
PP::PPMethods#pp, #pp_object, #pp_hash void same
PP::ObjectMixin#pretty_print_inspect String returns PP.singleline_pp(self, ''.dup)
PP.pp, PP.singleline_pp T (the out argument), or IO without out both return out, which defaults to $>

void accepts any implementation return value, so existing pretty_print definitions (e.g. the -> untyped ones in stdlib/openssl) stay compatible.

PP.pp and PP.singleline_pp are split into two overloads, as in Marshal.dump, because a single ?T out overload leaves T unbound when out is omitted:

def self.pp: (_PrettyPrint obj) -> IO
           | [T < _LeftShift] (_PrettyPrint obj, T out, ?Integer width) -> T

Test plan

  • bundle exec rake validate
  • test/stdlib/PP_test.rb (also with --enable-frozen-string-literal), with added cases for PP.pp without out and with width
  • bundle exec steep check
  • bundle exec rubocop test/stdlib/PP_test.rb

🤖 Generated with Claude Code

https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb

pretty_print, pretty_print_cycle, PP#pp, #pp_object and #pp_hash are
called for their side effects, so return void. pretty_print_inspect
returns String. PP.pp and PP.singleline_pp return their out argument,
or $> (IO) when it is omitted.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb

@ksss ksss left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@ksss
ksss added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ruby:master with commit 98d6033 Oct 15, 2026
25 checks passed
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.

2 participants