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 more types for Random (fix conflict #470) #1144

Merged
merged 6 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions core/random.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
# use SecureRandom for security purpose, instead of this PRNG.
#
class Random < RBS::Unnamed::Random_Base
# <!--
# rdoc-file=random.c
# - Random.new(seed = Random.new_seed) -> prng
# -->
# Creates a new PRNG using `seed` to set the initial state. If `seed` is
# omitted, the generator is initialized with Random.new_seed.
#
# See Random.srand for more information on the use of seed values.
#
def initialize: (?Integer seed) -> void

# <!--
# rdoc-file=random.c
# - Random.bytes(size) -> string
# -->
# Returns a random binary string. The argument `size` specifies the length of
# the returned string.
#
def self.bytes: (Integer size) -> String

# <!--
# rdoc-file=random.c
# - prng1 == prng2 -> true or false
Expand Down Expand Up @@ -71,6 +91,7 @@ class Random < RBS::Unnamed::Random_Base
def self.rand: () -> Float
| (Integer | ::Range[Integer] max) -> Integer
| (Float | ::Range[Float] max) -> Float
| (::Range[T]) -> T
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the type we want would be [T <: Numeric] (Range[T]) -> T, but RBS currently doesn't allow writing it.

I think there are two options. [T] (Range[T]) -> T or (Range[Numeric]) -> Numeric. The first option would make sense in my opinion, because testing the code will uncover the problem anyway.
#470 (comment)

Is [T] (Range[T]) invalid syntax?

Copy link
Member

Choose a reason for hiding this comment

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

[T] (Range[T]) is not a valid piece of code. It becomes a valid method type by adding the returned type, like [T] (Range[T]) -> T as soutaro said.

By the way, [T < Numeric] (Range[T]) -> T is more appropriate because the upper bound type for a type parameter has been implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pocke Thanks. Fixed at 224a180


# <!--
# rdoc-file=random.c
Expand All @@ -95,6 +116,52 @@ class Random < RBS::Unnamed::Random_Base
# [ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
#
def self.srand: (?Integer number) -> Integer

# <!--
# rdoc-file=random.c
# - Random.urandom(size) -> string
# -->
# Returns a string, using platform providing features. Returned value is
# expected to be a cryptographically secure pseudo-random number in binary form.
# This method raises a RuntimeError if the feature provided by platform failed
# to prepare the result.
#
# In 2017, Linux manpage random(7) writes that "no cryptographic primitive
# available today can hope to promise more than 256 bits of security". So it
# might be questionable to pass size > 32 to this method.
#
# Random.urandom(8) #=> "\x78\x41\xBA\xAF\x7D\xEA\xD8\xEA"
#
def self.urandom: (Integer) -> String

# <!--
# rdoc-file=random.c
# - prng.seed -> integer
# -->
# Returns the seed value used to initialize the generator. This may be used to
# initialize another generator with the same state at a later time, causing it
# to produce the same sequence of numbers.
#
# prng1 = Random.new(1234)
# prng1.seed #=> 1234
# prng1.rand(100) #=> 47
#
# prng2 = Random.new(prng1.seed)
# prng2.rand(100) #=> 47
#
def seed: () -> Integer

private

def initialize_copy: (self object) -> self

def left: () -> untyped

def marshal_dump: () -> untyped

def marshal_load: (untyped) -> untyped

def state: () -> untyped
end

Random::DEFAULT: Random
Expand Down
10 changes: 10 additions & 0 deletions test/stdlib/Random_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ def test_new
Random, :new
end

def test_bytes
assert_send_type "(::Integer size) -> ::String",
Random, :bytes, 0
end

def test_new_seed
assert_send_type "() -> ::Integer",
Random, :new_seed
end

def test_urandom
assert_send_type "(::Integer) -> ::String",
Random, :urandom, 0
end
end

class RandomTest < Test::Unit::TestCase
Expand Down