il_tonino
il_tonino

Reputation: 25

Python linters do not highlight undefined classes in imports

If I try to import any class that does not exist in python, Ruff linter does not highlight it

from numpy import ClassThatDoesNotExist
instance = ClassThatDoesNotExist()

(if I remove the import, Ruff will tell me that the class is not defined.)

I know that it's possible to modify Ruff rules but I cant find one that match this import problem.

I'm also using sonarLint but it does not highlight this error as well.

It's hard for me to believe that this rule is not supported by Ruff.

Does someone know the name of the rule or another linter that could find this error ? (except pylint, I find it way too slow)

Upvotes: 1

Views: 381

Answers (1)

il_tonino
il_tonino

Reputation: 25

I realised that if you want to check for these errors, you might want to use pylance.

pylance comes with the Python extension for Visual Studio Code, but sometimes it's disabled so you might have to enable it.

Then go to:

  • Python › Analysis: Type Checking Mode, and set it to basic.

This will check all your types and your imports. You can disable and enable different rules, look up to the extension.

If you also want to have type hints, add these options in settings.json :

{     
    "python.analysis.inlayHints.functionReturnTypes": true,
    "python.analysis.inlayHints.variableTypes": true,
}

Upvotes: 0

Related Questions