1

In VS Code, the Problems tab is showing errors when using os.path.join, but the code actually runs. How do I tell MyPy that these are not errors?

I'm working on the Salome platform, and the existing code that runs shows the following as an error: (the specific program is envSalome.py, which is the program that starts the whole salome-meca show):

import os

kernel_root=os.getenv("KERNEL_ROOT_DIR")
kernel_root = os.path.realpath(kernel_root)                 # this is line 39 in the error below
sys.path[:0]=[os.path.join(kernel_root,"bin","salome")]     # this is line 40 in the error below

This program runs without errors.

I've used the python interpreter link at the lower-left corner of VS Code to select the python3.exe that shipped with Salome (and which runs this code without errors).

My problem:
In the "Problems" tab of the Terminal window, MyPy is showing these errors:

Value of type variable "AnyStr" of "realpath" cannot be "Optional[str]" mypy(error) [39, 16]

Argument 1 to "join" has incompatible type "Optional[str]"; expected "Union[str, _PathLike[str]]" mypy(error) [40,29]

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • I have found a workaround: under vs code settings, search for "linting" and turn off all the linters, then install pylance from Microsoft, and it will perform linting even though it's not listed under "linting" settings: and pylance does not seem to flag these non-errors as errors (so far). – Vector Hasting Jan 02 '21 at 02:40

2 Answers2

4

os.getenv("KERNEL_ROOT_DIR") may return a str or None. You are not taking that into account and mypy is telling you that you did not cover the case when it returns None.

If you expect the value to always exist in the environment, I suggest using a function which does not return None, e.g.

kernel_root=os.environ["KERNEL_ROOT_DIR"]
zvone
  • 18,045
  • 3
  • 49
  • 77
1

MyPy is just doing what it's supposed to do: "type check your code and find common bugs.".

In this case, the error makes sense because os.getenv(key, default=None) returns "the value of the environment variable key if it exists, or default if it doesn’t.". In the event the KERNEL_ROOT_DIR environment variable is not defined, either the os.path realpath or join function will raise an Exception .

If you are 100% sure the KERNEL_ROOT_DIR variable will always exist in the environment where you will run this code, you can tell MyPy to ignore those errors. (We tell MyPy to ignore them because there is no way for MyPy to know about your Salome platform or to understand that they are not errors.)

There are a couple of ways.

Way 1

You can silence that specific error on those specific line/s by adding a special # type: ignore[code] comment on that line:

kernel_root = os.getenv("KERNEL_ROOT_DIR")
kernel_root = os.path.realpath(kernel_root)  # type: ignore[type-var]
sys.path[:0] = [os.path.join(kernel_root, "bin", "salome")]  # type: ignore[arg-type]

To get the exact MyPy error codes to show up on VS Code's Problems tab, add the following to your settings.json:

"python.linting.mypyArgs": [
    "--show-error-codes"
]

enter image description here

If you don't care about finding the specific error/s and you just want to silence all errors for that line, you can just drop the [code] part and just do # type: ignore.

Way 2

If you can isolate those lines into a specific module, you can tell MyPy to ignore all errors for that specific module. This can be done by creating a MyPy configuration file, which is a mypy.ini or a .mypy.ini or a setup.cfg file placed at the root of your workspace, then adding the ignore_errors option.

For example, if those lines are part of a module config.py inside the package myproject, having this MyPy configuration file will silence those errors:

[mypy-myproject.config]
ignore_errors = True

If you want to ignore only specific error codes from the configuration file, there is the disable_error_code option, but it is a global setting only:

[mypy]
disable_error_code = type-var

The sample above ignores one of the errors:

enter image description here

But note that this also ignores other errors on other lines on that same module.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135