Skip to content

Unused yet class verified when inside the module #19

Description

@developer88

Hi there.

I was test driving your awesome work and found one edge case:

require 'low_type'
require 'json'
require 'time'
require 'httparty'

module GithubFetcher
  class GithubResponse
    include LowType
    using LowType::Syntax

    type_reader code: Integer, json: Hash

    def initialize(code: Integer , json: Hash )
      @code = code
      @json = json
    end

    def success?() -> { TrueClass | FalseClass }
      (200..299).include?(code)
    end
  end

  class Fetcher
    include LowType
    using LowType::Syntax

    def fetch_response(url =  String | 'https://api.github.com/repos/ruby/ruby/releases/latest') -> { ::GithubFetcher::GithubResponse }
      http = HTTParty.get(url, headers: { 'User-Agent' => 'static-typing-demo' })
      ::GithubFetcher::GithubResponse.new(code: http.code.to_i, json: ::JSON.parse(http.body))
    end

    def fetch(printer = Proc | nil) -> { NilClass | nil }
      resp = fetch_response
      raise "HTTP #{resp.code}" unless resp.success?

      published_at = Time.parse((resp.json['published_at'] || Time.now.utc.iso8601).to_s)
      printer&.call((resp.json['tag_name'] || resp.json['name']).to_s, resp.json['html_url'], published_at)
      nil
    end
  end
end

PRINTER = proc { |tag, url, published_at|
  puts "Fetched tag=#{tag} published_at=#{published_at.utc.iso8601} (url=#{url})"
  nil
}

puts GithubFetcher::Fetcher.new.fetch(PRINTER) if $PROGRAM_NAME == __FILE__

This code generates the following error:

lib/expressions/type_expression.rb:46:in 'LowType::TypeExpression#validate!': Invalid argument type 'NilClass' for parameter 'code'. Valid types: 'Integer' (LowType::ArgumentTypeError)

        raise proxy.error_type, proxy.error_message(value:) if required?

If we move Fetcher and GithubResponse classes out of the module, then the error is gone. Also, even if I do not call the GithubResponse class at all, the error is still there. I use the latest version of the gem 1.1.6 under ruby 4.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions