Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obj.extend(Methods) should update type definition obj with the methods of Methods #1063

Open
HoneyryderChuck opened this issue Feb 28, 2024 · 3 comments

Comments

@HoneyryderChuck
Copy link

HoneyryderChuck commented Feb 28, 2024

this type of extension of type definitions isn't being done by steep on type analysis via steep check.

# given
class A
  def foo: () -> void
end

module B
  def bar: () -> void
end

# following code should be correctly checked
a = A.new
a.extend(B)
a.bar
@soutaro
Copy link
Owner

soutaro commented Mar 1, 2024

Same for #1064. You can annotate the type of the object using intersection type.

a = A.new
b = a.extend(B) #: A & B

@HoneyryderChuck
Copy link
Author

@soutaro steep does not support syntax to switch a variable's type mid-code. A similar but perhaps clearer example of what happens is:

# given type a: Array[Number | :stop]
a = [1, 2, :stop]

a.delete(:stop)

# and given def func: (Array[Number]) -> void
func(a)

steep will signal a type error on that last call, given that a is still an "array of numbers of stop" at that point, despite the operation above. adding an annotation beforehand like @type var a: Array[Number] won't help, as steep will apply that signature to a.delete(:stop), which will error from then on, despite it being placed before the annotation.

@soutaro
Copy link
Owner

soutaro commented Mar 19, 2024

The scope of type annotations is not very clear and might be confusing... Generally, it's based on the scope of local variables in Ruby. a is the same variable through the lines, so the @type annotation applies to all of the occurrence of a.

For this specific example, assigning to a itself with type assertion works.

a = [1, 2, :stop]

a.delete(:stop)

a = a #: Array[Integer]

# a is Array[Integer] here

(💡 It works even without delete call...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants