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

Support expansion of all path configuration options #7273

Merged
merged 2 commits into from Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/source/config_file.rst
Expand Up @@ -21,6 +21,10 @@ Most flags correspond closely to :ref:`command-line flags
<command-line>` but there are some differences in flag names and some
flags may take a different value based on the module being processed.

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}``.

Config file format
******************

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

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

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

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


Platform configuration
Expand Down Expand Up @@ -447,7 +454,8 @@ section of the command line docs.

``custom_typeshed_dir`` (string)
Specifies an alternative directory to look for stubs instead of the
default ``typeshed`` directory.
default ``typeshed`` directory. User home directory and environment
variables will be expanded.

``warn_incomplete_stub`` (bool, default False)
Warns about missing type annotations in typeshed. This is only relevant
Expand Down
18 changes: 13 additions & 5 deletions mypy/config_parser.py
Expand Up @@ -34,6 +34,14 @@ def parse_version(v: str) -> Tuple[int, int]:
return major, minor


def expand_path(path: str) -> str:
"""Expand the user home directory and any environment variables contained within
the provided path.
"""

return os.path.expandvars(os.path.expanduser(path))


def split_and_match_files(paths: str) -> List[str]:
"""Take a string representing a list of files/directories (with support for globbing
through the glob library).
Expand All @@ -45,7 +53,7 @@ def split_and_match_files(paths: str) -> List[str]:
expanded_paths = []

for path in paths.split(','):
path = path.strip()
path = expand_path(path.strip())
globbed_files = fileglob.glob(path, recursive=True)
if globbed_files:
expanded_paths.extend(globbed_files)
Expand All @@ -63,8 +71,8 @@ def split_and_match_files(paths: str) -> List[str]:
'python_version': parse_version,
'strict_optional_whitelist': lambda s: s.split(),
'custom_typing_module': str,
'custom_typeshed_dir': str,
'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)],
'custom_typeshed_dir': expand_path,
'mypy_path': lambda s: [expand_path(p.strip()) for p in re.split('[,:]', s)],
'files': split_and_match_files,
'quickstart_file': str,
'junit_xml': str,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would enabling environment variable interpolation for this config key be as simple as this?

Suggested change
'junit_xml': str,
'junit_xml': expand_path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would enabling environment variable interpolation for this config key be as simple as this?

Yes. Probably should have done this for junit_xml and quickstart_file as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'll include that in my PR #8479

Expand All @@ -75,6 +83,8 @@ def split_and_match_files(paths: str) -> List[str]:
'always_true': lambda s: [p.strip() for p in s.split(',')],
'always_false': lambda s: [p.strip() for p in s.split(',')],
'package_root': lambda s: [p.strip() for p in s.split(',')],
'cache_dir': expand_path,
'python_executable': expand_path,
} # type: Final


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