soutaro / steep

Static type checker for Ruby
MIT License
1.36k stars 83 forks source link

steep ignores assignments to variables through local scopes #1231

Open p-datadog opened 1 week ago

p-datadog commented 1 week ago

When a variable is overwritten from a block scope, steep ignores the overwrite. Example:

value = nil

loop do
  value = 1
  break
end

value + 2

puts value

Result:

% be steep check
# Type checking files:

...................................................................................F

test.rb:8:6: [error] Type `nil` does not have method `+`
│ Diagnostic ID: Ruby::NoMethod
│
└ value + 2
        ~

Detected 1 problem from 1 file

The above example is valid Ruby code. Is there a way to not have steep generate an error on it?

A more real-world usage example of the same pattern is Benchmark.realtime:

value = nil
time = Benchmark.realtime do
  value = ...
end