简体   繁体   中英

Is there a way to configure pylint (in vs code) to recognize methods imported through wildcards?

I would like to use a linter (specifically pylint) to debug my code, but accept wildcard imports.

At the moment, it highlights an error in the code, which is importing the gurobipy package:

Undefined variable 'Model' pylint(undefined-variable)

The code snippet provided highlights this issue:

from gurobipy import *

m = Model("mip1") # Pylint shows error in this line

I would like to know if pylint just cannot handle wildcard imports.

I've checked through pylint options in .pylintrc file, generated though console commands, but to no avail.

Yes, I am aware they are a bad coding practice, but I don't know how many methods of the package I'll end up using and I'd prefer avoiding to write "package.method" all the time.

Enabling jedi in the vs code options does allow intellisense to infer the package objects, but the error highlighting persists.

If it is not doable, I'd happily take a recommendation for another linter instead.

Of course it's better to import only the things you really need than importing everything and then have a lot of unused items. But for personal scripts or projects and practicing purposes it's not a big issue.

So, if you just want to prevent Pylint from displaying that specific warning, you can simply disable it by adding the following to your settings.json:

"python.linting.pylintArgs": [
    "--disable=wildcard-import"
]

You might want to take a look at this detailed answer to the question What exactly does "import * " import?

It should be able to detect modules through wildcard imports, depending on how the codes are structured/organized.

Here is a sample code structure:

|- main.py
|- mymodules
   |- __init__.py
   |- calculator.py

main.py

from mymodules import *

print(get_sum(1, 2))

mymodules/__init__.py

from .calculator import *

mymodules/calculator.py

def get_sum(op_1, op_2):
    return op_1 + op_2

That results in only the wildcard-import warning:

vscode

The only pylint settings I have is to disable UseMinimalCheckers and the missing-docstring warning:

"python.linting.pylintArgs": [
    "--disable=missing-docstring"
],
"python.linting.pylintUseMinimalCheckers": false,

I highly recommend not getting used to wildcard imports or finding workarounds to hide linter warnings. You are being warned for a reason. See Why is “import *” bad? . A good practice when starting out your app, is to start grouping your codes into modules with similar functionality, and then just importing each module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM