この記事は、ケーシーエスキャロット Advent Calendar 2020 23日目の記事です。
昨日は、naokishi の aws cli と jq コマンド でした。
今、AWS使ってるんですねー。もうWindowsアプリは開発していないのかな…?
さて、前回はエラーが増えたところで終わりました。
一度、リポジトリ にあるサンプルコードを実行してみようか…
ということで、コピペで実行してみると…
$ bundle exec steep check --log-level=fatal
lib/phone.rb:9:2: MethodBodyTypeMismatch: method===, expected=bool, actual=(bool | nil) (def ==(other))
(bool | nil) <: bool="" false="" nil="" true="="> nil <: bool="" false="" nil="" true="=">
エラーが出ました... が、一昨日出ていたエラーとは違うし、で、以下のようにコードを修正して実行してみると…
class User
def initialize(no:, name:)
@no = no
@name = name
@scores = Array.new
end
attr_reader :no, :name, :scores
def add(score:)
@scores << score
end
end
$ bundle exec steep check --log-level=fatal
bin/main.rb:20:2: NoMethodError: type=singleton(::File), method=open (File.open(fpath, "w") {|fd| fd.write data })
bin/main.rb:22:24: ArgumentTypeMismatch: receiver=singleton(::Analyzer), expected={ :path => untyped }, actual=::String (fpath)
lib/analyzer.rb:6:2: MethodArityMismatch: method=(self) (def self.load(path))
lib/analyzer.rb:15:2: MethodArityMismatch: method=(self) (def self.parse_row(row))
lib/analyzer.rb:16:8: IncompatibleArguments: receiver=singleton(::User), method_type=(name: untyped, no: untyped) -> ::User (User.new(row["No"], row["Name"]))
bin/main.rb:20:2: NoMethodError: type=singleton(::File), method=open (File.open(fpath, "w") {|fd| fd.write data })
bin/main.rb:22:24: ArgumentTypeMismatch: receiver=singleton(::Analyzer), expected={ :path => untyped }, actual=::String (fpath)
lib/analyzer.rb:16:8: IncompatibleArguments: receiver=singleton(::User), method_type=(name: untyped, no: untyped) -> ::User (User.new(row["No"], row["Name"]))
エラーが更に減った〜
出ているエラーが全部違うので、ここからは1つずつ確認していこう…
NoMethodError
bundle exec steep check --log-level=fatal --verbose --log-output="./check.log"
bin/main.rb:22:2: UnexpectedBlockGiven: method_type=((::string | ::_ToPath | ::int), ?(::string | ::int), ?::int) -> ::File (File.new(fpath, "w") {|fd| fd.write data })
bin/main.rb:25:24: ArgumentTypeMismatch: receiver=singleton(::Analyzer), expected={ :path => untyped }, actual=::String (fpath)
lib/analyzer.rb:16:8: IncompatibleArguments: receiver=singleton(::User), method_type=(name: untyped, no: untyped) -> ::User (User.new(row["No"], row["Name"]))
# rbs に File.open()が定義されていない
# File.open(fpath, "w") {|fd| fd.write data }
fd = File.new(fpath, "w")
fd.write data
fd.close
$ bundle exec steep check --log-level=fatal
bin/main.rb:26:24: ArgumentTypeMismatch: receiver=singleton(::Analyzer), expected={ :path => untyped }, actual=::String (fpath)
lib/analyzer.rb:16:8: IncompatibleArguments: receiver=singleton(::User), method_type=(name: untyped, no: untyped) -> ::User (User.new(row["No"], row["Name"]))
ArgumentTypeMismatch, IncompatibleArgument
rbsファイルに型を定義する
class User
@no: Integer
@name: String
@scores: Array[untyped]
def initialize: (no: Integer, name: String) -> untyped
def add: (score: untyped) -> untyped
end
class Analyzer
def self.load: (path: String) -> Array[User]
def self.parse_row: (row: CSV::Row) -> User
end
これで実行すると、、、
$ bundle exec steep check --log-level=fatal
sig/sample.rbs:11:28...11:36 UnknownTypeNameError: name=::CSV::Row
CSVクラスは、標準添付ライブラリのため、requireして使っているので、Steepfile のlibrary で追加します。
target :app do
signature "sig"
check "bin"
check "lib"
library "csv"
end
そして実行すると、、、
$ bundle exec steep check --log-level=fatal
lib/analyzer.rb:9:30: IncompatibleAssignment: lhs_type=::CSV::Row, rhs_type=::Array[(::String | nil)] (row)
::Array[(::String | nil)] <: ::basicobject="" ::csv::row="=" ::object=""> ::BasicObject <: ::csv::row="" code="" does="" hold="" not="">
やっとそれっぽいエラーが出てきました。
class CSV < Object
def self.foreach: [U] (String, ?::Hash[Symbol, U] options) { (CSV::Row arg0) -> void } -> void
end
class CSV::Row < Object
alias [] field
end
実行してみると、、、





