rtype 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +72 -33
- data/lib/rtype/core_ext.rb +7 -3
- data/lib/rtype/version.rb +1 -1
- data/lib/rtype.rb +32 -9
- data/spec/spec_helper.rb +3 -0
- metadata +15 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 573badbc833de876d782b2e6c3fb21a0ca10cd49
|
|
4
|
+
data.tar.gz: ebd29e696a8752c8f79ac4867d24ac63cedbd72b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ddda1bff5a6d6f2af36d738fecd36b74f148c912de27d2f4b7c85f8f95d235eade4000746e0ab418075b9817671f734faa0a0882dc96d75f3fff995aa9b977fc
|
|
7
|
+
data.tar.gz: 74da35dc41d1b3c4380a9a405ef3ee68a87ded82eaf99269ea35fc1d504e7bfde5fa96495eeffe7fe9e218c1c03d902b7781894b2ca5472104944543d832f0e3
|
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Rtype: ruby with type
|
|
2
|
-
[](https://badge.fury.io/rb/rtype)
|
|
2
|
+
[](https://badge.fury.io/rb/rtype)
|
|
3
|
+
[](https://travis-ci.org/sputnikgugja/rtype)
|
|
4
|
+
[](https://coveralls.io/github/sputnikgugja/rtype?branch=master)
|
|
3
5
|
|
|
4
6
|
You can do the type checking in Ruby with this gem!
|
|
5
7
|
|
|
@@ -352,69 +354,106 @@ Example.new.method(:test).return_type
|
|
|
352
354
|
# => Any
|
|
353
355
|
```
|
|
354
356
|
|
|
357
|
+
## Documentation
|
|
358
|
+
[RubyDoc.info](http://www.rubydoc.info/gems/rtype)
|
|
359
|
+
|
|
355
360
|
## Benchmarks
|
|
356
361
|
Result of `rake benchmark` ([source](https://github.com/sputnikgugja/rtype/tree/master/benchmark/benchmark.rb))
|
|
357
362
|
|
|
358
363
|
The benchmark doesn't include `Rubype` gem because I can't install Rubype on my environment.
|
|
359
364
|
|
|
360
365
|
### MRI
|
|
366
|
+
Rtype is 1.56x faster than Sig and 2.85x faster than Contracts
|
|
367
|
+
|
|
361
368
|
```
|
|
362
369
|
Ruby version: 2.1.7
|
|
363
370
|
Ruby engine: ruby
|
|
364
371
|
Ruby description: ruby 2.1.7p400 (2015-08-18 revision 51632) [x64-mingw32]
|
|
365
|
-
Rtype version: 0.
|
|
372
|
+
Rtype version: 0.1.1
|
|
366
373
|
Sig version: 1.0.1
|
|
367
374
|
Contracts version: 0.13.0
|
|
368
375
|
Typecheck version: 0.1.2
|
|
369
376
|
Warming up --------------------------------------
|
|
370
|
-
pure
|
|
371
|
-
rtype
|
|
372
|
-
sig
|
|
373
|
-
contracts
|
|
374
|
-
typecheck
|
|
377
|
+
pure 52.239k i/100ms
|
|
378
|
+
rtype 7.861k i/100ms
|
|
379
|
+
sig 5.680k i/100ms
|
|
380
|
+
contracts 3.514k i/100ms
|
|
381
|
+
typecheck 813.000 i/100ms
|
|
375
382
|
Calculating -------------------------------------
|
|
376
|
-
pure
|
|
377
|
-
rtype
|
|
378
|
-
sig
|
|
379
|
-
contracts
|
|
380
|
-
typecheck
|
|
383
|
+
pure 2.125M (±25.5%) i/s - 10.030M
|
|
384
|
+
rtype 106.900k (±13.8%) i/s - 526.687k
|
|
385
|
+
sig 68.405k (±17.0%) i/s - 335.120k
|
|
386
|
+
contracts 37.494k (±12.1%) i/s - 186.242k
|
|
387
|
+
typecheck 9.165k (±11.7%) i/s - 45.528k
|
|
381
388
|
|
|
382
389
|
Comparison:
|
|
383
|
-
pure:
|
|
384
|
-
rtype:
|
|
385
|
-
sig:
|
|
386
|
-
contracts:
|
|
387
|
-
typecheck:
|
|
390
|
+
pure: 2124989.5 i/s
|
|
391
|
+
rtype: 106899.8 i/s - 19.88x slower
|
|
392
|
+
sig: 68405.0 i/s - 31.06x slower
|
|
393
|
+
contracts: 37493.9 i/s - 56.68x slower
|
|
394
|
+
typecheck: 9164.5 i/s - 231.87x slower
|
|
388
395
|
```
|
|
389
396
|
|
|
390
397
|
### JRuby
|
|
398
|
+
Rtype is 1.23x ~ 1.90x faster than Sig and 3.26x ~ 4.57x faster than Contracts
|
|
399
|
+
|
|
400
|
+
```
|
|
401
|
+
# Test 1
|
|
402
|
+
Ruby version: 2.2.3
|
|
403
|
+
Ruby engine: jruby
|
|
404
|
+
Ruby description: jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [Windows 10-amd64]
|
|
405
|
+
Rtype version: 0.1.1
|
|
406
|
+
Sig version: 1.0.1
|
|
407
|
+
Contracts version: 0.13.0
|
|
408
|
+
Typecheck version: 0.1.2
|
|
409
|
+
Warming up --------------------------------------
|
|
410
|
+
pure 7.415k i/100ms
|
|
411
|
+
rtype 883.000 i/100ms
|
|
412
|
+
sig 922.000 i/100ms
|
|
413
|
+
contracts 408.000 i/100ms
|
|
414
|
+
typecheck 404.000 i/100ms
|
|
415
|
+
Calculating -------------------------------------
|
|
416
|
+
pure 4.242M (±46.3%) i/s - 16.795M
|
|
417
|
+
rtype 55.772k (±29.1%) i/s - 249.006k
|
|
418
|
+
sig 29.344k (±23.5%) i/s - 134.612k
|
|
419
|
+
contracts 12.192k (±29.4%) i/s - 51.816k
|
|
420
|
+
typecheck 7.503k (±23.5%) i/s - 35.148k
|
|
421
|
+
|
|
422
|
+
Comparison:
|
|
423
|
+
pure: 4241753.6 i/s
|
|
424
|
+
rtype: 55771.7 i/s - 76.06x slower
|
|
425
|
+
sig: 29344.3 i/s - 144.55x slower
|
|
426
|
+
contracts: 12192.0 i/s - 347.91x slower
|
|
427
|
+
typecheck: 7502.8 i/s - 565.36x slower
|
|
428
|
+
```
|
|
391
429
|
```
|
|
430
|
+
# Test 2
|
|
392
431
|
Ruby version: 2.2.3
|
|
393
432
|
Ruby engine: jruby
|
|
394
433
|
Ruby description: jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [Windows 10-amd64]
|
|
395
|
-
Rtype version: 0.
|
|
434
|
+
Rtype version: 0.1.1
|
|
396
435
|
Sig version: 1.0.1
|
|
397
436
|
Contracts version: 0.13.0
|
|
398
437
|
Typecheck version: 0.1.2
|
|
399
438
|
Warming up --------------------------------------
|
|
400
|
-
pure
|
|
401
|
-
rtype
|
|
402
|
-
sig
|
|
403
|
-
contracts
|
|
404
|
-
typecheck
|
|
439
|
+
pure 7.943k i/100ms
|
|
440
|
+
rtype 1.060k i/100ms
|
|
441
|
+
sig 1.565k i/100ms
|
|
442
|
+
contracts 365.000 i/100ms
|
|
443
|
+
typecheck 545.000 i/100ms
|
|
405
444
|
Calculating -------------------------------------
|
|
406
|
-
pure
|
|
407
|
-
rtype
|
|
408
|
-
sig
|
|
409
|
-
contracts
|
|
410
|
-
typecheck
|
|
445
|
+
pure 4.321M (±48.9%) i/s - 17.220M
|
|
446
|
+
rtype 56.584k (±22.5%) i/s - 266.060k
|
|
447
|
+
sig 45.925k (±17.8%) i/s - 223.795k
|
|
448
|
+
contracts 17.338k (±20.8%) i/s - 82.490k
|
|
449
|
+
typecheck 8.449k (±16.5%) i/s - 41.420k
|
|
411
450
|
|
|
412
451
|
Comparison:
|
|
413
|
-
pure:
|
|
414
|
-
rtype:
|
|
415
|
-
sig:
|
|
416
|
-
contracts:
|
|
417
|
-
typecheck:
|
|
452
|
+
pure: 4320949.8 i/s
|
|
453
|
+
rtype: 56584.5 i/s - 76.36x slower
|
|
454
|
+
sig: 45925.4 i/s - 94.09x slower
|
|
455
|
+
contracts: 17337.9 i/s - 249.22x slower
|
|
456
|
+
typecheck: 8449.4 i/s - 511.39x slower
|
|
418
457
|
```
|
|
419
458
|
|
|
420
459
|
## Rubype, Sig
|
data/lib/rtype/core_ext.rb
CHANGED
|
@@ -39,20 +39,24 @@ private
|
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
class Method
|
|
42
|
+
def typed?
|
|
43
|
+
!!::Rtype.type_signatures[owner][name]
|
|
44
|
+
end
|
|
45
|
+
|
|
42
46
|
def type_signature
|
|
43
47
|
::Rtype.type_signatures[owner][name]
|
|
44
48
|
end
|
|
45
49
|
|
|
46
50
|
def type_info
|
|
47
|
-
|
|
51
|
+
::Rtype.type_signatures[owner][name].info
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
def argument_type
|
|
51
|
-
|
|
55
|
+
::Rtype.type_signatures[owner][name].argument_type
|
|
52
56
|
end
|
|
53
57
|
|
|
54
58
|
def return_type
|
|
55
|
-
|
|
59
|
+
::Rtype.type_signatures[owner][name].return_type
|
|
56
60
|
end
|
|
57
61
|
end
|
|
58
62
|
|
data/lib/rtype/version.rb
CHANGED
data/lib/rtype.rb
CHANGED
|
@@ -45,13 +45,13 @@ module Rtype
|
|
|
45
45
|
sig.return_type = return_sig
|
|
46
46
|
@@type_signatures[owner][method_name] = sig
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
# `send` is faster than `method(...).call`
|
|
49
|
+
owner.send(:_rtype_proxy).send :define_method, method_name do |*args, **kwargs, &block|
|
|
49
50
|
if kwargs.empty?
|
|
50
51
|
::Rtype.assert_arguments_type(expected_args, args)
|
|
51
52
|
result = super(*args, &block)
|
|
52
53
|
else
|
|
53
|
-
::Rtype.
|
|
54
|
-
::Rtype.assert_keyword_arguments_type(expected_kwargs, kwargs)
|
|
54
|
+
::Rtype.assert_arguments_type_with_keywords(expected_args, args, expected_kwargs, kwargs)
|
|
55
55
|
result = super(*args, **kwargs, &block)
|
|
56
56
|
end
|
|
57
57
|
::Rtype.assert_return_type(return_sig, result)
|
|
@@ -70,8 +70,6 @@ module Rtype
|
|
|
70
70
|
# validate argument type
|
|
71
71
|
def valid?(expected, value)
|
|
72
72
|
case expected
|
|
73
|
-
when Rtype::Behavior::Base
|
|
74
|
-
expected.valid? value
|
|
75
73
|
when Module
|
|
76
74
|
value.is_a? expected
|
|
77
75
|
when Symbol
|
|
@@ -91,6 +89,8 @@ module Rtype
|
|
|
91
89
|
!!value
|
|
92
90
|
when false
|
|
93
91
|
!value
|
|
92
|
+
when Rtype::Behavior::Base
|
|
93
|
+
expected.valid? value
|
|
94
94
|
else
|
|
95
95
|
raise TypeSignatureError, "Invalid type signature: Unknown type behavior #{expected}"
|
|
96
96
|
end
|
|
@@ -101,16 +101,18 @@ module Rtype
|
|
|
101
101
|
end
|
|
102
102
|
|
|
103
103
|
def assert_arguments_type(expected_args, args)
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
# `length.times` is faster than `each_with_index`
|
|
105
|
+
args.length.times do |i|
|
|
106
|
+
expected = expected_args[i]
|
|
107
|
+
value = args[i]
|
|
106
108
|
unless expected.nil?
|
|
107
109
|
unless valid?(expected, value)
|
|
108
|
-
raise ArgumentTypeError, "for #{(
|
|
110
|
+
raise ArgumentTypeError, "for #{(i+1).ordinalize} argument:\n" + type_error_message(expected, value)
|
|
109
111
|
end
|
|
110
112
|
end
|
|
111
113
|
end
|
|
112
114
|
end
|
|
113
|
-
|
|
115
|
+
=begin
|
|
114
116
|
def assert_keyword_arguments_type(expected_kwargs, kwargs)
|
|
115
117
|
kwargs.each do |key, value|
|
|
116
118
|
expected = expected_kwargs[key]
|
|
@@ -121,6 +123,27 @@ module Rtype
|
|
|
121
123
|
end
|
|
122
124
|
end
|
|
123
125
|
end
|
|
126
|
+
=end
|
|
127
|
+
def assert_arguments_type_with_keywords(expected_args, args, expected_kwargs, kwargs)
|
|
128
|
+
# `length.times` is faster than `each_with_index`
|
|
129
|
+
args.length.times do |i|
|
|
130
|
+
expected = expected_args[i]
|
|
131
|
+
value = args[i]
|
|
132
|
+
unless expected.nil?
|
|
133
|
+
unless valid?(expected, value)
|
|
134
|
+
raise ArgumentTypeError, "for #{(i+1).ordinalize} argument:\n" + type_error_message(expected, value)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
kwargs.each do |key, value|
|
|
139
|
+
expected = expected_kwargs[key]
|
|
140
|
+
unless expected.nil?
|
|
141
|
+
unless valid?(expected, value)
|
|
142
|
+
raise ArgumentTypeError, "for '#{key}' argument:\n" + type_error_message(expected, value)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
124
147
|
|
|
125
148
|
def assert_return_type(expected, result)
|
|
126
149
|
if expected.nil?
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rtype
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sputnik Gugja
|
|
@@ -38,6 +38,20 @@ dependencies:
|
|
|
38
38
|
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: coveralls
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
41
55
|
description: Ruby with type
|
|
42
56
|
email:
|
|
43
57
|
- sputnikgugja@gmail.com
|