Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to pass in ENV variables? #25

Open
halfnibble opened this issue Mar 4, 2021 · 9 comments · May be fixed by #101
Open

Any way to pass in ENV variables? #25

halfnibble opened this issue Mar 4, 2021 · 9 comments · May be fixed by #101

Comments

@halfnibble
Copy link

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.
@matangover
Copy link
Owner

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
Copy link
Owner

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

@dave-mclean
Copy link

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?

@eupharis
Copy link

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.)

@matangover matangover reopened this Mar 14, 2023
@matangover
Copy link
Owner

matangover commented Mar 14, 2023

@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?

@j-osephlong
Copy link

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'

@matangover
Copy link
Owner

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

@dave-mclean
Copy link

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

@danlamanna
Copy link

danlamanna commented Oct 11, 2024

@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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants