Sorbet won't allow Zlib::GzipReader as a parameter to a method

35 views Asked by At

This Ruby code with a Sorbet signature:

sig { params(source: T.any(String, Zlib::GzipReader)).returns(T::Boolean) }
def file?(source)
  source.is_a?(String) && File.exist?(source.to_s)
end

generates the following error:

Malformed type declaration. Generic class without type arguments Zlib::GzipReader https://srb.help/5045

The revealed type for source is Zlib::GzipReader[T.anything] but if I specify that I get a different error:

 Method [] does not exist on T.class_of(Zlib::GzipReader)[Zlib::GzipReader[T.anything]] https://srb.help/7003

I think this is the relevant line in zlib.rbi and it has something to do with Covariance but I'm not sure how to make it work.

1

There are 1 answers

0
paracycle On

This has the same root cause as the one that prompted this PR upstream into Sorbet: https://github.com/sorbet/sorbet/pull/6324

I think you can open a similar PR to fix the type variable of Zlib::GzipReader to T.untyped (or maybe even String, since Zlib::GzipReader#each seems to yield string instances to the supplied block).