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

Inherited attributes reassignment not detected properly. #1430

Closed
kenzocarneiro opened this issue Aug 2, 2022 · 0 comments
Closed

Inherited attributes reassignment not detected properly. #1430

kenzocarneiro opened this issue Aug 2, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@kenzocarneiro
Copy link

Describe the bug
I have a class with a property named str. I also have a subclass which inherits from that class.
When reassigning the property of the subclass to its own value, it breaks the type detection. This bug only happens on inherited properties.

Here is an example with code:

--- MyClass
--- @class MyClass
--- @field str string
MyClass = {}
MyClass.str = "My str"

--- Constructor of MyClass.
--- @return MyClass
function MyClass:new()
    local e = {}
    setmetatable(e, self)
    self.__index = self
    return e
end

--- MySubClass
--- @class MySubClass : MyClass
MySubClass = MyClass:new()
MySubClass.str2 = "My str2"

-- This works:
MyClass.str = MyClass.str
MySubClass.str2 = MySubClass.str2
-- This doesn't work:
MySubClass.str = MySubClass.str

print(MySubClass.str) -- Prints "My str"

The value of MySubClass.str is shown as "unknown":
image

This is a trivial example, but in other contexts it can completely break type checking.

To reproduce

  • Create a class MyClass with an attribute str of a given type.
  • Create a subclass MySubClass inheriting from that class.
  • Assign the attribute of MySubClass to itself: MySubClass.str = MySubClass.str

Expected behavior
MySubClass.str should still be recognized as a string after a reassignment to itself.

Environment:

  • OS: Windows 10 x64
  • Is WSL remote? No
  • Client: VSCode

Temporary Fix
I also found a temporary fix for this issue: repeating @field str string in every subclass:

--- MySubClass
--- @class MySubClass : MyClass
--- @field str string
MySubClass = MyClass:new()

But this isn't ideal...

@sumneko sumneko added the bug Something isn't working label Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants