Skip to content

Commit c6d6be4

Browse files
committed
Support expansion of all path configuration options
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 56ed5c3 commit c6d6be4

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def split_and_match_files(paths: str) -> List[str]:
4646

4747
for path in paths.split(','):
4848
path = path.strip()
49+
path = os.path.expandvars(os.path.expanduser(path))
4950
globbed_files = fileglob.glob(path, recursive=True)
5051
if globbed_files:
5152
expanded_paths.extend(globbed_files)
@@ -64,7 +65,8 @@ def split_and_match_files(paths: str) -> List[str]:
6465
'strict_optional_whitelist': lambda s: s.split(),
6566
'custom_typing_module': str,
6667
'custom_typeshed_dir': str,
67-
'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)],
68+
'mypy_path': lambda s: [os.path.expandvars(os.path.expanduser(p.strip()))
69+
for p in re.split('[,:]', s)],
6870
'files': split_and_match_files,
6971
'quickstart_file': str,
7072
'junit_xml': str,
@@ -75,6 +77,9 @@ def split_and_match_files(paths: str) -> List[str]:
7577
'always_true': lambda s: [p.strip() for p in s.split(',')],
7678
'always_false': lambda s: [p.strip() for p in s.split(',')],
7779
'package_root': lambda s: [p.strip() for p in s.split(',')],
80+
'cache_dir': lambda s: os.path.expandvars(os.path.expanduser(s)),
81+
'custom_typeshed_dir': lambda s: os.path.expandvars(os.path.expanduser(s)),
82+
'python_executable': lambda s: os.path.expandvars(os.path.expanduser(s)),
7883
} # type: Final
7984

8085

@@ -223,8 +228,6 @@ def parse_section(prefix: str, template: Options,
223228
except ValueError as err:
224229
print("%s%s: %s" % (prefix, key, err), file=stderr)
225230
continue
226-
if key == 'cache_dir':
227-
v = os.path.expandvars(os.path.expanduser(v))
228231
if key == 'silent_imports':
229232
print("%ssilent_imports has been replaced by "
230233
"ignore_missing_imports=True; follow_imports=skip" % prefix, file=stderr)

0 commit comments

Comments
 (0)