Module: Rtype

Extended by:
Rtype
Included in:
Rtype
Defined in:
lib/rtype.rb,
lib/rtype/version.rb,
lib/rtype/behavior.rb,
lib/rtype/rtype_proxy.rb,
lib/rtype/behavior/and.rb,
lib/rtype/behavior/not.rb,
lib/rtype/behavior/xor.rb,
lib/rtype/behavior/base.rb,
lib/rtype/type_signature.rb,
lib/rtype/behavior/nilable.rb,
lib/rtype/method_annotator.rb,
lib/rtype/return_type_error.rb,
lib/rtype/behavior/typed_set.rb,
lib/rtype/argument_type_error.rb,
lib/rtype/behavior/typed_hash.rb,
lib/rtype/behavior/float_check.rb,
lib/rtype/behavior/typed_array.rb,
lib/rtype/type_signature_error.rb,
lib/rtype/behavior/integer_check.rb,
lib/rtype/behavior/numeric_check.rb

Defined Under Namespace

Modules: Behavior, MethodAnnotator Classes: ArgumentTypeError, ReturnTypeError, RtypeProxy, TypeSignature, TypeSignatureError

Constant Summary collapse

VERSION =
"0.6.8".freeze
JAVA_EXT_VERSION =

rtype java extension version. nil If the extension is not used

nil
NATIVE_EXT_VERSION =

rtype c extension version. nil If the extension is not used

nil
@@type_signatures =

This is just ‘information’ Any change of this doesn’t affect type checking

Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.and(*args) ⇒ Object



21
22
23
# File 'lib/rtype/behavior/and.rb', line 21

def self.and(*args)
	Behavior::And[*args]
end

Instance Method Details

#arg_message(idx) ⇒ String

Returns:

  • (String)


159
160
161
# File 'lib/rtype.rb', line 159

def arg_message(idx)
	"for #{ordinalize_number(idx+1)} argument:"
end

#arg_type_error_message(idx, expected, value) ⇒ String

Returns A error message.

Parameters:

  • idx (Integer)
  • expected

    A type behavior

  • value

Returns:

  • (String)

    A error message

Raises:

  • (ArgumentError)

    If expected is invalid



144
145
146
# File 'lib/rtype.rb', line 144

def arg_type_error_message(idx, expected, value)
	"#{arg_message(idx)}\n" + type_error_message(expected, value)
end

#assert_arguments_type(expected_args, args) ⇒ void

This method returns an undefined value.

Validates arguments

Parameters:

  • expected_args (Array)

    A type signature for non-keyword arguments

  • args (Array)

Raises:



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/rtype.rb', line 350

def assert_arguments_type(expected_args, args)
	e_len = expected_args.length
	# `length.times` is faster than `each_with_index`
	args.length.times do |i|
		break if i >= e_len
		expected = expected_args[i]
		value = args[i]
		unless valid?(expected, value)
			raise ArgumentTypeError, "#{arg_message(i)}\n" + type_error_message(expected, value)
		end
	end
	nil
end

#assert_arguments_type_with_keywords(expected_args, args, expected_kwargs, kwargs) ⇒ void

This method returns an undefined value.

Validates arguments and keyword arguments

Parameters:

  • expected_args (Array)

    A type signature for non-keyword arguments

  • args (Array)

    Arguments

  • expected_kwargs (Hash)

    A type signature for keyword arguments

  • kwargs (Hash)

    Keword arguments

Raises:



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/rtype.rb', line 376

def assert_arguments_type_with_keywords(expected_args, args, expected_kwargs, kwargs)
	e_len = expected_args.length
	# `length.times` is faster than `each_with_index`
	args.length.times do |i|
		break if i >= e_len
		expected = expected_args[i]
		value = args[i]
		unless valid?(expected, value)
			raise ArgumentTypeError, "#{arg_message(i)}\n" + type_error_message(expected, value)
		end
	end
	
	kwargs.each do |key, value|
		if expected_kwargs.key?(key)
			expected = expected_kwargs[key]
			unless valid?(expected, value)
				raise ArgumentTypeError, "#{kwarg_message(key)}\n" + type_error_message(expected, value)
			end
		end
	end
	nil
end

#assert_return_type(expected, result) ⇒ Object



409
410
411
412
413
414
# File 'lib/rtype.rb', line 409

def assert_return_type(expected, result)
	unless valid?(expected, result)
		raise ReturnTypeError, "for return:\n" + type_error_message(expected, result)
	end
	nil
end

#assert_valid_argument_type_sig_element(sig) ⇒ Object

Checks the type behavior is valid

Parameters:

  • sig

    A type behavior

Raises:



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/rtype.rb', line 272

def assert_valid_argument_type_sig_element(sig)
	case sig
	when Rtype::Behavior::Base
	when Module
	when Symbol
	when Regexp
	when Range
	when Array
		sig.each do |e|
			assert_valid_argument_type_sig_element(e)
		end
	when Hash
		sig.each_value do |e|
			assert_valid_argument_type_sig_element(e)
		end
	when Proc
	when true
	when false
	when nil
	else
		raise TypeSignatureError, "Invalid type signature: Unknown type behavior #{sig}"
	end
end

#assert_valid_arguments_type_sig(sig) ⇒ Object

Checks the arguments type signature is valid

e.g. ‘[Integer]`, `“value”` are valid. `Integer` is invalid

Parameters:

  • sig

    A arguments type signature

Raises:



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rtype.rb', line 245

def assert_valid_arguments_type_sig(sig)
	if sig.is_a?(Array)
		sig = sig.dup
		if sig.last.is_a?(Hash)
			kwargs = sig.pop
		else
			kwargs = {}
		end
		sig.each { |e| assert_valid_argument_type_sig_element(e) }
		if kwargs.keys.any? { |e| !e.is_a?(Symbol) }
			raise TypeSignatureError, "Invalid type signature: keyword arguments contain non-symbol key"
		end
		kwargs.each_value { |e| assert_valid_argument_type_sig_element(e) }
	elsif sig.is_a?(Hash)
		if sig.keys.any? { |e| !e.is_a?(Symbol) }
			raise TypeSignatureError, "Invalid type signature: keyword arguments contain non-symbol key"
		end
		sig.each_value { |e| assert_valid_argument_type_sig_element(e) }
	else
		raise TypeSignatureError, "Invalid type signature: arguments type signature is neither array nor hash"
	end
end

#assert_valid_return_type_sig(sig) ⇒ Object



297
298
299
# File 'lib/rtype.rb', line 297

def assert_valid_return_type_sig(sig)
	assert_valid_argument_type_sig_element(sig)
end

#assert_valid_type_sig(sig) ⇒ Object

Checks the type signature is valid

e.g. ‘[Integer] => Any` is valid. `[Integer]` or `Any` are invalid

Parameters:

  • sig

    A type signature

Raises:



226
227
228
229
230
231
232
233
234
235
# File 'lib/rtype.rb', line 226

def assert_valid_type_sig(sig)
	unless sig.is_a?(Hash)
		raise TypeSignatureError, "Invalid type signature: type signature is not hash"
	end
	if sig.empty?
		raise TypeSignatureError, "Invalid type signature: type signature is empty hash"
	end
	assert_valid_arguments_type_sig(sig.first[0])
	assert_valid_return_type_sig(sig.first[1])
end

#define_typed_accessor(owner, name, type_behavior) ⇒ void

This method returns an undefined value.

Parameters:

  • owner

    Owner of the accessor

  • name (#to_sym)
  • type_behavior

    A type behavior. e.g. Integer

Raises:



85
86
87
88
# File 'lib/rtype.rb', line 85

def define_typed_accessor(owner, name, type_behavior)
	define_typed_reader(owner, name, type_behavior)
	define_typed_writer(owner, name, type_behavior)
end

#define_typed_method(owner, method_name, type_sig_info) ⇒ void

This method returns an undefined value.

Makes the method typed

Parameters:

  • owner

    Owner of the method

  • method_name (#to_sym)
  • type_sig_info (Hash)

    A type signature. e.g. ‘[Integer, Float] => Float`

Raises:

  • (ArgumentError)

    If method_name is nil

  • (TypeSignatureError)

    If type_sig_info is invalid



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rtype.rb', line 46

def define_typed_method(owner, method_name, type_sig_info)
	method_name = method_name.to_sym
	raise ArgumentError, "method_name is nil" if method_name.nil?
	assert_valid_type_sig(type_sig_info)

	el = type_sig_info.first
	arg_sig = el[0]
	return_sig = el[1]

	if arg_sig.is_a?(Array)
		expected_args = arg_sig.dup
		if expected_args.last.is_a?(Hash)
			expected_kwargs = expected_args.pop
		else
			expected_kwargs = {}
		end
	elsif arg_sig.is_a?(Hash)
		expected_args = []
		expected_kwargs = arg_sig
	end

	sig = TypeSignature.new
	sig.argument_type = arg_sig
	sig.return_type = return_sig
	unless @@type_signatures.key?(owner)
		@@type_signatures[owner] = {}
	end
	@@type_signatures[owner][method_name] = sig

	define_typed_method_to_proxy(owner, method_name, expected_args, expected_kwargs, return_sig)
end

#define_typed_reader(owner, name, type_behavior) ⇒ void

This method returns an undefined value.

Parameters:

  • owner

    Owner of the getter

  • name (#to_sym)
  • type_behavior

    A type behavior. e.g. Integer

Raises:



97
98
99
100
101
# File 'lib/rtype.rb', line 97

def define_typed_reader(owner, name, type_behavior)
	raise ArgumentError, "name is nil" if name.nil?
	valid?(type_behavior, nil)
	define_typed_method owner, name.to_sym, [] => type_behavior
end

#define_typed_writer(owner, name, type_behavior) ⇒ void

This method returns an undefined value.

Parameters:

  • owner

    Owner of the setter

  • name (#to_sym)
  • type_behavior

    A type behavior. e.g. Integer

Raises:



110
111
112
113
114
# File 'lib/rtype.rb', line 110

def define_typed_writer(owner, name, type_behavior)
	raise ArgumentError, "name is nil" if name.nil?
	valid?(type_behavior, nil)
	define_typed_method owner, :"#{name.to_sym}=", [type_behavior] => Any
end

#kwarg_message(key) ⇒ String

Returns:

  • (String)


164
165
166
# File 'lib/rtype.rb', line 164

def kwarg_message(key)
	"for '#{key}' argument:"
end

#kwarg_type_error_message(key, expected, value) ⇒ String

Returns A error message.

Parameters:

  • key (String, Symbol)
  • expected

    A type behavior

  • value

Returns:

  • (String)

    A error message

Raises:

  • (ArgumentError)

    If expected is invalid



154
155
156
# File 'lib/rtype.rb', line 154

def kwarg_type_error_message(key, expected, value)
	"#{kwarg_message(key)}\n" + type_error_message(expected, value)
end

#nilable(*args) ⇒ Object



18
19
20
# File 'lib/rtype/behavior/nilable.rb', line 18

def nilable(*args)
	Behavior::Nilable[*args]
end

#not(*args) ⇒ Object



21
22
23
# File 'lib/rtype/behavior/not.rb', line 21

def not(*args)
	Behavior::Not[*args]
end

#type_error_message(expected, value) ⇒ String

Note:

This method doesn’t check the value is valid

Returns a error message for the pair of type behavior and value

Parameters:

  • expected

    A type behavior

  • value

Returns:

  • (String)

    error message

Raises:



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rtype.rb', line 176

def type_error_message(expected, value)
	case expected
	when Rtype::Behavior::Base
		expected.error_message(value)
	when Module
		"Expected #{value.inspect} to be a #{expected}"
	when Symbol
		"Expected #{value.inspect} to respond to :#{expected}"
	when Regexp
		"Expected stringified #{value.inspect} to match regexp #{expected.inspect}"
	when Range
		"Expected #{value.inspect} to be included in range #{expected.inspect}"
	when Array
		arr = expected.map { |e| type_error_message(e, value) }
		arr.join("\nOR ")
	when Hash
		if value.is_a?(Hash)
			arr = []
			expected.each do |k, v|
				if v.is_a?(Array) || v.is_a?(Hash)
					arr << "- #{k} : {\n" + type_error_message(v, value[k]) + "\n}"
				else
					arr << "- #{k} : " + type_error_message(v, value[k])
				end
			end
			"Expected #{value.inspect} to be a hash with #{expected.length} elements:\n" + arr.join("\n")
		else
			"Expected #{value.inspect} to be a hash"
		end
	when Proc
		"Expected #{value.inspect} to return a truthy value for proc #{expected}"
	when true
		"Expected #{value.inspect} to be a truthy value"
	when false
		"Expected #{value.inspect} to be a falsy value"
	when nil # for return
		"Expected #{value.inspect} to be nil"
	else
		raise TypeSignatureError, "Invalid type behavior #{expected}"
	end
end

#type_signaturesHash

Note:

type_signatures[method_name]

This is just ‘information’ Any change of this doesn’t affect type checking

Returns:



121
122
123
# File 'lib/rtype.rb', line 121

def type_signatures
	@@type_signatures
end

#valid?(expected, value) ⇒ Boolean

Checks the value is valid for the type behavior

Parameters:

  • expected

    A type behavior

  • value

Returns:

Raises:



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/rtype.rb', line 309

def valid?(expected, value)
	case expected
	when Module
		value.is_a? expected
	when Symbol
		value.respond_to? expected
	when Regexp
		!!(expected =~ value.to_s)
	when Range
		expected.include?(value)
	when Hash
		return false unless value.is_a?(Hash)
		return false unless expected.keys == value.keys
		expected.all? { |k, v| valid?(v, value[k]) }
	when Array
		expected.any? { |e| valid?(e, value) }
	when Proc
		!!expected.call(value)
	when true
		!!value
	when false
		!value
	when Rtype::Behavior::Base
		expected.valid? value
	when nil
		value.nil?
	else
		raise TypeSignatureError, "Invalid type signature: Unknown type behavior #{expected}"
	end
end

#xor(*args) ⇒ Object



22
23
24
# File 'lib/rtype/behavior/xor.rb', line 22

def xor(*args)
	Behavior::Xor[*args]
end