ruby / rbs

Type Signature for Ruby
Other
1.91k stars 201 forks source link

%a{pure} does not seem to work for attr_reader #1872

Closed HoneyryderChuck closed 3 weeks ago

HoneyryderChuck commented 3 weeks ago

I see that the %a{pure} label can be added to def to indicate the return value of a function is pure (which helps in type narrowing), but the same does not seem to work for attribute readers:


# class A
#   %a{pure} attr_reader b: String?
# end

class A
  attr_reader :b
 def initialize(b = nil); @b = b; end
end

a = A.new("bang")

if a.b
  # steep still consider b to be String, not String?
ParadoxV5 commented 3 weeks ago

Did you mean? Steep still consider b to be String?, not String

I’ve reproduced in Steep 1.7 but it is able to type-narrow using the if statement with or without %a{pure}. OTOH, Steep does not type narrow without the if statement by inferring from #initialize


I get Cannot find implementation of method `::A#b` somehow, but it does not impact the topic.

ParadoxV5 commented 3 weeks ago

Aren’t attr_XXX be pure by definition? unless they’re used by lazy people to write RBS for their custom accessors

HoneyryderChuck commented 3 weeks ago

you're correct, it's already pure. I got confused by the message.