Skip to content

Add support for user-specific config file #4939

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

Merged
merged 1 commit into from
Apr 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,10 @@ Here are some more useful flags:

- ``--config-file CONFIG_FILE`` causes configuration settings to be
read from the given file. By default settings are read from ``mypy.ini``
or ``setup.cfg`` in the current directory. Settings override mypy's
built-in defaults and command line flags can override settings.
See :ref:`config-file` for the syntax of configuration files.
or ``setup.cfg`` in the current directory, or ``.mypy.ini`` in the user home
directory. Settings override mypy's built-in defaults and command line flags
can override settings. See :ref:`config-file` for the syntax of configuration
files.

- ``--junit-xml JUNIT_XML`` will make mypy generate a JUnit XML test
result document with type checking results. This can make it easier
Expand Down
7 changes: 4 additions & 3 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ The mypy configuration file
===========================

Mypy supports reading configuration settings from a file. By default
it uses the file ``mypy.ini`` (with fallback to ``setup.cfg``) in the
current directory; the ``--config-file`` command-line flag can be used to
read a different file instead (see :ref:`--config-file <config-file-flag>`).
it uses the file ``mypy.ini`` with fallback to ``setup.cfg`` in the current
directory, or ``.mypy.ini`` in the user home directory if none of them are
found; the ``--config-file`` command-line flag can be used to read a different
file instead (see :ref:`--config-file <config-file-flag>`).

It is important to understand that there is no merging of configuration
files, as it would lead to ambiguity. The ``--config-file`` flag
Expand Down
3 changes: 3 additions & 0 deletions mypy/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
PYTHON3_VERSION_MIN = (3, 3)
CACHE_DIR = '.mypy_cache'
CONFIG_FILE = 'mypy.ini'
SHARED_CONFIG_FILES = ('setup.cfg',)
USER_CONFIG_FILES = ('~/.mypy.ini',)
CONFIG_FILES = (CONFIG_FILE,) + SHARED_CONFIG_FILES + USER_CONFIG_FILES
11 changes: 4 additions & 7 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def add_invertible_flag(flag: str,
help="Script x becomes module x instead of __main__")
parser.add_argument('--config-file',
help="Configuration file, must have a [mypy] section "
"(defaults to {})".format(defaults.CONFIG_FILE))
"(defaults to {})".format(', '.join(defaults.CONFIG_FILES)))
add_invertible_flag('--show-column-numbers', default=False,
help="Show column numbers in error messages")
parser.add_argument('--find-occurrences', metavar='CLASS.MEMBER',
Expand Down Expand Up @@ -677,21 +677,18 @@ def add_invertible_flag(flag: str,
'always_false': lambda s: [p.strip() for p in s.split(',')],
}

SHARED_CONFIG_FILES = ('setup.cfg',)


def parse_config_file(options: Options, filename: Optional[str]) -> None:
"""Parse a config file into an Options object.

Errors are written to stderr but are not fatal.

If filename is None, fall back to default config file and then
to setup.cfg.
If filename is None, fall back to default config files.
"""
if filename is not None:
config_files = (filename,) # type: Tuple[str, ...]
else:
config_files = (defaults.CONFIG_FILE,) + SHARED_CONFIG_FILES
config_files = tuple(map(os.path.expanduser, defaults.CONFIG_FILES))

parser = configparser.RawConfigParser()

Expand All @@ -710,7 +707,7 @@ def parse_config_file(options: Options, filename: Optional[str]) -> None:
return

if 'mypy' not in parser:
if filename or file_read not in SHARED_CONFIG_FILES:
if filename or file_read not in defaults.SHARED_CONFIG_FILES:
print("%s: No [mypy] section in config file" % file_read, file=sys.stderr)
else:
section = parser['mypy']
Expand Down