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 pp in stdlib #1545

Merged
merged 5 commits into from
Oct 30, 2023
Merged

Add pp in stdlib #1545

merged 5 commits into from
Oct 30, 2023

Conversation

ksss
Copy link
Collaborator

@ksss ksss commented Sep 27, 2023

I added the type for the standard library pp since it was missing.

I am performing the following tasks also:

@ksss
Copy link
Collaborator Author

ksss commented Sep 27, 2023

It seems there might be an issue with the RBS::Writer.

# original
def seplist: (untyped list, ?(^() -> void)? sep, ?interned iter_method) { (*untyped, **untyped) -> void } -> void

# RBS::Writer
def seplist: (untyped list, ?^() -> void? sep, ?interned iter_method) { (*untyped, **untyped) -> void } -> void

The following RBS is interpreted as next follows:

^() -> void?
^() -> (void | nil)

core/kernel.rbs Outdated
@@ -1269,7 +1269,7 @@ module Kernel : BasicObject
# pp returns argument(s).
#
def self?.pp: [T] (T arg0) -> T
| (untyped, untyped, *untyped) -> Array[untyped]
| (untyped, *untyped) -> Array[untyped]
Copy link
Member

Choose a reason for hiding this comment

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

The second untyped was actually to skip the first overload if one argument is given.
Not sure if removing it causes another type error or not. (It won't on Steep.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I see, that's what it meant.

The original definition is as follows:

module Kernel
  def pp(*objs)
    objs.each {|obj|
      PP.pp(obj)
    }
    objs.size <= 1 ? objs.first : objs
  end
  module_function :pp
end

I became concerned because Steep reported the following:

The method parameter has different kind from the declaration `[T(353)(354)] (?untyped, ?untyped, *untyped) -> (T(353)(354) | ::Array[untyped] | nil)`(Ruby::DifferentMethodParameterKind)

Copy link
Member

Choose a reason for hiding this comment

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

@ksss I mean the use-site. It depends on how the overloading is resolved. (Steep uses the first matching one, so passing one argument to pp resolves to the first overload anyways.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK. I'll revert this commit later.

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

👍

@soutaro soutaro added this to the RBS 3.3 milestone Sep 27, 2023
@soutaro
Copy link
Member

soutaro commented Oct 30, 2023

@ksss I found that pp is automatically loaded today. Moving to core would make more sense?

@ksss
Copy link
Collaborator Author

ksss commented Oct 30, 2023

@soutaro Thank you. Certainly, that's true.
I believe pp should be treated the same way as set.

I will move it to the core soon.

@ksss
Copy link
Collaborator Author

ksss commented Oct 30, 2023

@soutaro
Oh wait.

If we move pp to the core, then its dependency pretty_print must also be moved to the core.
Looking at https://github.com/ruby/ruby/blob/5c1b7633fcd9c7c43974b72bd36cab1f9e9bd186/prelude.rb, we can see that while Set is autoloaded, but PP and PrettyPrint are not.
This might be because PP and PrettyPrint are used less frequently than Set.

It seems that pp is not exactly treated the same as set.

Moreover, the signature for pp is already included in the core.

rbs/core/kernel.rbs

Lines 1262 to 1272 in 32cd6c9

# <!--
# rdoc-file=lib/pp.rb
# - pp(*objs)
# -->
# prints arguments in pretty form.
#
# pp returns argument(s).
#
def self?.pp: [T] (T arg0) -> T
| (untyped, untyped, *untyped) -> Array[untyped]
| () -> nil

I believe it might be better not to add pp to the core.

@soutaro
Copy link
Member

soutaro commented Oct 30, 2023

@ksss Got it.

#pp (method) can be used without explicitly loading it. PP (the library) should be loaded manually. It looks reasonable.

Clicking the merge button. 👍

@soutaro soutaro added this pull request to the merge queue Oct 30, 2023
Merged via the queue into ruby:master with commit df08f99 Oct 30, 2023
@ksss ksss deleted the pp branch October 30, 2023 07:09
@soutaro soutaro added the Released PRs already included in the released version label Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

2 participants