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

Can‘t recognize class variable #2365

Closed
treert opened this issue Feb 10, 2022 · 4 comments
Closed

Can‘t recognize class variable #2365

treert opened this issue Feb 10, 2022 · 4 comments
Labels
waiting for user response Requires more information from user

Comments

@treert
Copy link

treert commented Feb 10, 2022

Environment data

  • Language Server version: v2022.2.1
  • OS and version: Win10 19042.1466
  • Python version : 3.10.0 64-bit

Expected behaviour

example code:
image

A1,A2 shoudle able to jump to declare position

Actual behaviour

  • Just can't jump, and recognize aa.A1 as any.
  • Not highlight

Additional information

sample code:

class A:
    def __init__(self,a:int = 0) -> None:
        self.a = a

    def __str__(self) -> str:
        return f"A({self.a})"
# A1
A.A1 = A(1)
# A2
A.A2 = A(2)

if __name__ == "__main__":
    print(A.A1)
    print(A.A2)
    aa = A(3)
    print(aa.A1)

related issue

#1064

@erictraut
Copy link
Contributor

This pattern is unusual. You're assigning instances of class A as class variables on class A. Is that your intent? That's not something I would recommend from a design perspective.

For a static analyzer to honor a class variable, you need to declare it within the class definition. Normally you could simply include the assignment (A = A(1)) within the class body, but in this case you cannot because of the circular dependency. You therefore need to declare the class variables with a forward declaration, like this:

class A:
    A1: "A"
    A2: "A"

    def __init__(self, a: int = 0) -> None:
        self.a = a

    def __str__(self) -> str:
        return f"A({self.a})"

A.A1 = A(1)
A.A2 = A(2)

@judej judej added the waiting for user response Requires more information from user label Feb 10, 2022
@github-actions github-actions bot removed the triage label Feb 10, 2022
@treert
Copy link
Author

treert commented Feb 11, 2022

llvm-project/**/bindings/**/cindex.py write code in the way,
Search CursorKind.CXX_METHOD = CursorKind(21), can goto the position.
https://github.com/llvm/llvm-project/blob/main/clang/bindings/python/clang/cindex.py
This is where i rearch this problem.

Any way, will pylance support this pattern in the future?

@erictraut
Copy link
Contributor

If you mean "will Pylance ever recognize this as a class variable when you haven't declared the class variable within the class?", the answer is no.

@judej
Copy link
Contributor

judej commented Mar 1, 2022

Closing inactive issue, please reopen if needed

@judej judej closed this as completed Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests

3 participants