egonSchiele / contracts.ruby

Contracts for Ruby.
http://egonschiele.github.com/contracts.ruby
BSD 2-Clause "Simplified" License
1.44k stars 82 forks source link

KeywordArgs deficiencies? #282

Open indigoviolet opened 6 years ago

indigoviolet commented 6 years ago

The KeywordArgs contract appears to have two deficiencies:

1) A missing arg with a Maybe[] contract is valid despite not being declared optional

[9] development (main)> kk = Contracts::KeywordArgs[a: Contracts::Maybe[String]]
[10] development (main)> kk.valid?({})
=> true

In practice, this is often not an issue because the function definition would be like

Contract Contracts::KeywordArgs[a: Contracts::Maybe[String]]
def foo(a: )
end

and Ruby would enforce the presence of a.

However this would break in the more esoteric case of

Contract Contracts::KeywordArgs[a: Contracts::Maybe[String]]
def foo(**kwargs)
end

2) Default values for optional args are not validated by Contracts

[6] development (main)> module Foo
[6] development (main)*   Contract KeywordArgs[a: Optional[String]] => Any
[6] development (main)*   def self.foo(a: 2)
[6] development (main)*   end
[6] development (main)* end

[7] development (main)> Foo.foo
=> nil