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

mypy.ini processing should allow variable expansion ("interpolation") #6476

Closed
jtmoon79 opened this issue Feb 25, 2019 · 6 comments
Closed

Comments

@jtmoon79
Copy link

jtmoon79 commented Feb 25, 2019

tl;dr I cannot write variable-expanded directory paths within mypy.ini to set the mypy_path. I cannot commit a useful mypy.ini. It would be cool if mypy supported variable expansion ("interpolation") within it's configparser processed file.


Problem example

Given my pipenv-created virtualenv at C:/Users/user1/.virtualenvs/project1-rAPKj25Z, I want to set mypy_path to the virtual environment's Lib/site-packages. I can set

[mypy]
mypy_path: C:/Users/user1/.virtualenvs/project1-rAPKj25Z/Lib/site-packages

but that is not portable (i.e. committable). I would like to write something portable like

[mypy]
mypy_path: %(VIRTUAL_ENV)s/Lib/site-packages

where VIRTUAL_ENV is set by pipenv within the caller's environment (or perhaps a python-dotenv file).

...

This is a feature request. I am not sure if this is outside the expectations for mypy.ini files. However, it seems like a clean way to have more portable mypy.ini files.

Using

  • Windows 10 Pro
  • Python 3.7.1
  • mypy 0.670
  • pipenv 2018.11.26
@jtmoon79
Copy link
Author

jtmoon79 commented Feb 25, 2019

I thought this could easily be solved by

--- a/mypy/main.py                                                                                                                                                                                                                                                                      
+++ b/mypy/main.py
@@ -905,7 +905,7 @@ def parse_config_file(options: Options, filename: Optional[str]) -> None:                                                                                                                                                                                        
    else:
        config_files = tuple(map(os.path.expanduser, defaults.CONFIG_FILES))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
-    parser = configparser.RawConfigParser()                                                                                                                                                                                                                                        
+    parser = configparser.ConfigParser(os.environ)

given mypy.ini

[mypy]
mypy_path: %(VIRTUAL_ENV)s/Lib/site-packages

that solution worked in toy program

import configparser, os
config = configparser.ConfigParser(os.environ)
config.read("./mypy.ini")
print(config['mypy']['mypy_path'])  # prints C:/Users/user1/.virtualenvs/project1-rAPKj25Z/Lib/site-packages

I tried out that solution within mypy/main.py and it resulted in numerous errors mypy.ini: [mypy]: Unrecognized option: ..., e.g.

mypy.ini: [mypy]: Unrecognized option: aclocal_path = C:\Programs\Git\mingw64\share\aclocal;C:\Programs\Git\usr\share\aclocal
mypy.ini: [mypy]: Unrecognized option: allusersprofile = C:\ProgramData
...

Darn.

Looking closer at the code, the change may not be so trivial.


Relates to commit 2fbb724.

@JelleZijlstra
Copy link
Member

For context, do other development tools with config files (e.g., linters) usually support interpolating environment variables? That could help decide whether this is something worth supporting in mypy.

@jtmoon79
Copy link
Author

jtmoon79 commented Feb 25, 2019

do other development tools with config files (e.g., linters) usually support interpolating environment variables?

@JelleZijlstra this Issue might be broken out into two questions:

  • should mypy support configparser interpolating the .ini file?
  • how to make .ini file parameter mypy_path more portable (i.e. committable)?

Regarding the mypy_path, it strikes me as necessary to allow some method for inputting "portable paths".

@wakemaster39
Copy link

This would be a really helpful feature for situations like pipenv where I need to have a wrapper script to make sure mypy knows where to look for packages to avoid import errors and to ensure that the config is portable for the rest of my team.

@ilevkivskyi
Copy link
Member

This is now supported (in most cases), see https://mypy.readthedocs.io/en/latest/config_file.html:

Some flags support user home directory and environment variable expansion. To refer to the user home directory, use ~ at the beginning of the path. To expand environment variables use $VARNAME or ${VARNAME}.

@Mattwmaster58
Copy link
Contributor

@ilevkivskyi

Some flags support user home directory and environment variable expansion

Any hint as to what flags those are?

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

No branches or pull requests

5 participants