user70587
user70587

Reputation: 83

use python importlib causes lost autocomplete and lint

First. Some context. I have a class that helps to import several modules, as this will be done dynamically it is important to use the importlib library, so far everything works fine. BUT ... autocomplete capability is completely lost and the lint is not responding (vs-code).

The question is: How do I recover the autocomplete and the lint?

Here is the example code:


# ----------- file foo.py
import typing, types, importlib

print("foo was imported!")

class foo () : 
    def __init__ ( self ) : pass
    def foo_get_doble( self, number : int ) -> int : return number*2
    def foo_print_hello_word( self ) -> None : print("hello world")


# ----------- file __init__.py
import typing, sys, importlib

if __package__ : 
    template_module = importlib.import_module( f"{__package__}.foo" )
else : 
    print( "NO PKG" )
    sys.path.append( r"C:\working_folder" )
    foo_mod = importlib.import_module( "foo" )

importlib.reload( foo_mod )
# foo_class = foo_mod.  # NO HINTS NO LINT NO TYPE CHECK NOTHING (vs-code)
foo_class = foo_mod.foo() # but its there. works.

#output
# NO PKG
# foo was imported!
# foo was imported!

What I expect after the import :

enter image description here

What I get :

enter image description here

EDIT

after a long search. It seems that importlib does not return the custom module as such. But a TypeModule or something. Which is a kind of generic module class. When I get anything from the module, I get such a TypeAny. However, knowing this does not solve the problem. How I cast the type. or as a fill in the module so that the classes and methods are recognized?

Upvotes: 2

Views: 1048

Answers (2)

user70587
user70587

Reputation: 83

After a lot of research. It's impossible. Because the data is created in runtime. So... VSCode (or any editor) can get the information before hand. Is only possible after the creation, after the execution. The more near solution is use a Jupyter Notebook, but is no suitable for larges files.

Upvotes: 1

Jill Cheng
Jill Cheng

Reputation: 10372

In VS Code, Python's Linting function and auto-completion function are provided by the Python extension. Therefore, it is recommended that you reinstall the python extension and reload VS Code.

For the use of the python code analysis tool, take Pylint as an example, please install the module "pylint" in the python environment you are currently using, and then run it.

enter image description here

Update:

When I use "from de.foo import fo" to import the method fo() in the file "foo.py", "class_foo.f" shows the auto-completion option:

enter image description here

Since VS Code starts from the parent folder of the current file by default when searching for files, as an example to ensure the code runs, I added the code

import sys
sys.path.append("./")

, which adds the file path to the system path to help VS Code find it.

Reference: Linting in VS Code.

Upvotes: 1

Related Questions