Skip to content

Commit d044c2e

Browse files
hetmankpmsullivan
authored andcommitted
Support expansion of all path configuration options (#7273)
This commit modifies parsing of all configuration options dealing with paths so that they may be able to expand user home directories and environment variables as per the 'cache_dir' configuration.
1 parent f30aa79 commit d044c2e

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

docs/source/config_file.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Most flags correspond closely to :ref:`command-line flags
2121
<command-line>` but there are some differences in flag names and some
2222
flags may take a different value based on the module being processed.
2323

24+
Some flags support user home directory and environment variable expansion.
25+
To refer to the user home directory, use ``~`` at the beginning of the path.
26+
To expand environment variables use ``$VARNAME`` or ``${VARNAME}``.
27+
2428
Config file format
2529
******************
2630

@@ -355,7 +359,8 @@ a list of import discovery options that may be used
355359

356360
``python_executable`` (string)
357361
Specifies the path to the Python executable to inspect to collect
358-
a list of available :ref:`PEP 561 packages <installed-packages>`. Defaults to
362+
a list of available :ref:`PEP 561 packages <installed-packages>`. User
363+
home directory and environment variables will be expanded. Defaults to
359364
the executable used to run mypy.
360365

361366
``no_silence_site_packages`` (bool, default False)
@@ -366,13 +371,15 @@ a list of import discovery options that may be used
366371
``mypy_path`` (string)
367372
Specifies the paths to use, after trying the paths from ``MYPYPATH`` environment
368373
variable. Useful if you'd like to keep stubs in your repo, along with the config file.
374+
Multiple paths are always separated with a ``:`` or ``,`` regardless of the platform.
375+
User home directory and environment variables will be expanded.
369376

370377
``files`` (string)
371378
A comma-separated list of paths which should be checked by mypy if none are given on the command
372379
line. Supports recursive file globbing using
373380
[the glob library](https://docs.python.org/3/library/glob.html), where `*` (e.g. `*.py`) matches
374381
files in the current directory and `**/` (e.g. `**/*.py`) matches files in any directories below
375-
the current one.
382+
the current one. User home directory and environment variables will be expanded.
376383

377384

378385
Platform configuration
@@ -447,7 +454,8 @@ section of the command line docs.
447454

448455
``custom_typeshed_dir`` (string)
449456
Specifies an alternative directory to look for stubs instead of the
450-
default ``typeshed`` directory.
457+
default ``typeshed`` directory. User home directory and environment
458+
variables will be expanded.
451459

452460
``warn_incomplete_stub`` (bool, default False)
453461
Warns about missing type annotations in typeshed. This is only relevant

mypy/config_parser.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def parse_version(v: str) -> Tuple[int, int]:
3434
return major, minor
3535

3636

37+
def expand_path(path: str) -> str:
38+
"""Expand the user home directory and any environment variables contained within
39+
the provided path.
40+
"""
41+
42+
return os.path.expandvars(os.path.expanduser(path))
43+
44+
3745
def split_and_match_files(paths: str) -> List[str]:
3846
"""Take a string representing a list of files/directories (with support for globbing
3947
through the glob library).
@@ -45,7 +53,7 @@ def split_and_match_files(paths: str) -> List[str]:
4553
expanded_paths = []
4654

4755
for path in paths.split(','):
48-
path = path.strip()
56+
path = expand_path(path.strip())
4957
globbed_files = fileglob.glob(path, recursive=True)
5058
if globbed_files:
5159
expanded_paths.extend(globbed_files)
@@ -63,8 +71,8 @@ def split_and_match_files(paths: str) -> List[str]:
6371
'python_version': parse_version,
6472
'strict_optional_whitelist': lambda s: s.split(),
6573
'custom_typing_module': str,
66-
'custom_typeshed_dir': str,
67-
'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)],
74+
'custom_typeshed_dir': expand_path,
75+
'mypy_path': lambda s: [expand_path(p.strip()) for p in re.split('[,:]', s)],
6876
'files': split_and_match_files,
6977
'quickstart_file': str,
7078
'junit_xml': str,
@@ -75,6 +83,8 @@ def split_and_match_files(paths: str) -> List[str]:
7583
'always_true': lambda s: [p.strip() for p in s.split(',')],
7684
'always_false': lambda s: [p.strip() for p in s.split(',')],
7785
'package_root': lambda s: [p.strip() for p in s.split(',')],
86+
'cache_dir': expand_path,
87+
'python_executable': expand_path,
7888
} # type: Final
7989

8090

@@ -223,8 +233,6 @@ def parse_section(prefix: str, template: Options,
223233
except ValueError as err:
224234
print("%s%s: %s" % (prefix, key, err), file=stderr)
225235
continue
226-
if key == 'cache_dir':
227-
v = os.path.expandvars(os.path.expanduser(v))
228236
if key == 'silent_imports':
229237
print("%ssilent_imports has been replaced by "
230238
"ignore_missing_imports=True; follow_imports=skip" % prefix, file=stderr)

0 commit comments

Comments
 (0)