mypy-vscode icon indicating copy to clipboard operation
mypy-vscode copied to clipboard

Any way to pass in ENV variables?

Open halfnibble opened this issue 3 years ago • 8 comments

Basically, my python code reads environment variables.

Locally, I can get this working by setting them in my session. I even created a bash script to auto set them.

However, mypy (installed via the instructions for Mac) does not seem to find my environment variables, which leads to errors like "SESSION_NAME" not defined, etc.

What I tried:

  • Adding a source call to my env setting bash script in the ~/.mypyls/bin/activate script.

halfnibble avatar Mar 04 '21 10:03 halfnibble

Hey @halfnibble, for some reason I missed this issue, sorry. Does this problem happen also when you run mypy outside of VSCode? If yes, then this issue is not related to mypy-vscode, but to mypy itself. If no, then can you please include the relevant portion of your code so that I can reproduce the issue?

matangover avatar Apr 09 '21 08:04 matangover

Hi, I'm closing this issue for now, please reopen if needed.

matangover avatar May 03 '21 16:05 matangover

Can I re-open this?

I need to get vs-code to run mypy with certain envvars set. If I run mypy outside of VS-Code it will fail unless certain envvars are set.

python -m mypy.dmypy run .
> envparse.ConfigurationError: Environment variable 'ENVVAR' not set.

Outside of VS-Code it's easy to set the envvars:

export ENVVAR=somevar; python -m mypy.dmypy run .

but when VS-Code runs the same mypy command it fails because the envvars are not set. How can get VS-Code to load some envvars being running the mypy command?

dave-mclean avatar Mar 10 '23 14:03 dave-mclean

one way to handle this in VSCode:

Set the "Python: Env File" setting for your workspace:

image

Then mypy will run with all those environment variables present.

(Along with all your other Python plugins.)

eupharis avatar Mar 10 '23 15:03 eupharis

@dave-mclean ok, re-opened! Can you explain your use case? Why do you need environment vars to be set when mypy is run? As far as I know mypy shouldn't be importing your code so how come it's getting this error? Can you show me a minimal example to reproduce the issue?

matangover avatar Mar 14 '23 16:03 matangover

In my case, I use the django plugin, which when it runs my app.settings file fails on a key error for the environment variable 'VIRTUAL_ENV'

j-osephlong avatar May 13 '23 22:05 j-osephlong

Ah, ok. Interesting use case. I could add this feature quite easily

matangover avatar May 22 '23 10:05 matangover

sorry for the slow response!

Yes, this is exactly it. We use the django-plugin which imports settings, which in-turn tries to access an envvar. I could refactor my app but it looks like it's easy enough to give mypy the envvars it needs

dave-mclean avatar Mar 20 '24 14:03 dave-mclean

@matangover

Can you show me a minimal example to reproduce the issue?

FWIW this is a minimal example:

# reproducer.py
from mypy.plugin import Plugin
import os

class CustomPlugin(Plugin):
    def get_function_hook(self, fullname):
        os.environ["FOO"]  # attempts to access the env during type checking

def plugin(version: str):
    return CustomPlugin
# mypy.ini
[mypy]
plugins = reproducer.py

Now executing mypy --show-traceback on any file, including itself, results in:

Traceback (most recent call last):
  File "mypy/checker.py", line 591, in accept
  File "mypy/nodes.py", line 1183, in accept
  File "mypy/checker.py", line 2448, in visit_class_def
  File "mypy/checkexpr.py", line 1561, in check_call
  File "mypy/checkexpr.py", line 1800, in check_callable_call
  File "mypy/plugin.py", line 855, in get_function_hook
  File "mypy/plugin.py", line 897, in _find_hook
  File "mypy/plugin.py", line 855, in <lambda>
  File "/home/reproducer.py", line 7, in get_function_hook
    os.environ["FOO"]
    ~~~~~~~~~~^^^^^^^
  File "<frozen os>", line 685, in __getitem__
KeyError: 'FOO'

danlamanna avatar Oct 11 '24 06:10 danlamanna