Home > Blockchain >  How to "go to definition" to a method of super class in VSCode
How to "go to definition" to a method of super class in VSCode

Time:01-06

I am familiar with Pycharm and new to VSCode. I would like to "go to definition" like I did in Pycharm but I can't figure out how to. Btw I set up the environment in WSL2 Ubuntu and code from local with WSL VSCode extension.

Directory tree view is like :
src
 |-calculation
    |-base_class.py
    |-particular_class.py
 |-db_related_module
    |-db_class.py

and each module has:

base_class.py

class BaseClass():

    def __init__(self, DbClass):
        self.db_cls = DbClass

particular_class.py

from base_class import BaseClass
from db_related_module.db_class import DbClass


class ParticularCalClass(BaseClass):

    def __init__(self):
        super().__init__(
            DbClass
        )

    def some_calculation(self):
        do_db_stuff = self.db_cls
        ret = do_db_stuff.execute_sql()
        return ret


def main():
    calc_cls = ParticularCalClass()
    print(calc_cls.some_calculation())

main()

db_class.py

class DbClass():

    def __init__(self):
        pass

    def execute_sql():
        return "execute some sql"

Here I would like to jump from particular_class.py

ret = do_db_stuff.execute_sql()

to db_class.py

def execute_sql():

as it is the definition.

CodePudding user response:

Normally that would be using F12, or right click and go to definition.

CodePudding user response:

After testing this is the same problem on my machine. So I filed a report on GitHub and here is their response:

The import in line from base_class import BaseClass is incorrect in this scenario. It should be from calculation.base_class import BaseClass. And you will need to have PYTHONPATH=./src. If you have from base_class import BaseClass python won't know where the base_class module is, so you won't see go to definition working. you will wither have to add calculation directory to sys.path or change the import as I recommended above.

  • Related