mypy-vscode
mypy-vscode copied to clipboard
Any way to pass in ENV variables?
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.
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?
Hi, I'm closing this issue for now, please reopen if needed.
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?
one way to handle this in VSCode:
Set the "Python: Env File" setting for your workspace:
Then mypy will run with all those environment variables present.
(Along with all your other Python plugins.)
@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?
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'
Ah, ok. Interesting use case. I could add this feature quite easily
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
@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'