Skip to content
/ mypy Public
forked from python/mypy

Commit

Permalink
Allows for exclude as a multiline basic string in TOML files
Browse files Browse the repository at this point in the history
Multiple regexes are expressed as a sequence.

Fixes python#11825.
  • Loading branch information
posita committed Dec 22, 2021
1 parent f96446c commit 435c1a9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,31 @@ section of the command line docs.

This option may only be set in the global section (``[mypy]``).

.. note::

Note that the format TOML is slightly different. It can be either a single string
(including a multi-line string) -- which is treated as a single regular
expression -- or an array of such strings. The following TOML examples are
equivalent to the above INI file.

.. code-block:: toml
[tool.mypy]
exclude = [
"^file1\.py$",
"^file2\.py$",
]
.. code-block:: toml
[tool.mypy]
exclude = """(?x)(
^file1\.py$
|^file2\.py$",
)"""
See :ref:`using-a-pyproject-toml`.

.. confval:: namespace_packages

:type: boolean
Expand Down Expand Up @@ -907,6 +932,8 @@ These options may only be set in the global section (``[mypy]``).
Controls how much debug output will be generated. Higher numbers are more verbose.


.. _using-a-pyproject-toml:

Using a pyproject.toml file
***************************

Expand Down Expand Up @@ -965,6 +992,10 @@ of your repo (or append it to the end of an existing ``pyproject.toml`` file) an
python_version = "2.7"
warn_return_any = true
warn_unused_configs = true
exclude = [
"^file1\.py$",
"^file2\.py$",
]
# mypy per-module options:
Expand Down
7 changes: 7 additions & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def expand_path(path: str) -> str:
return os.path.expandvars(os.path.expanduser(path))


def str_or_seq_of_strs_as_list_of_strs(v: Union[str, Sequence[str]]) -> List[str]:
if isinstance(v, str):
return [v.strip()] if v.strip() else []
return [p.strip() for p in v if p.strip()]


def split_and_match_files_list(paths: Sequence[str]) -> List[str]:
"""Take a list of files/directories (with support for globbing through the glob library).
Expand Down Expand Up @@ -143,6 +149,7 @@ def check_follow_imports(choice: str) -> str:
'disable_error_code': try_split,
'enable_error_code': try_split,
'package_root': try_split,
'exclude': str_or_seq_of_strs_as_list_of_strs,
})


Expand Down
30 changes: 30 additions & 0 deletions test-data/unit/cmdline.pyproject.test
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,33 @@ def g(a: int) -> int:
[out]
pyproject.toml: toml config file contains [[tool.mypy.overrides]] sections with conflicting values. Module 'x' has two different values for 'disallow_untyped_defs'
== Return code: 0

[case testMultilineExcludePyprojectTOML]
# cmd: mypy x
[file pyproject.toml]
\[tool.mypy]
exclude = """(?x)(
(^|/)[^/]*skipme_\\.py$
|(^|/)_skipme[^/]*\\.py$
)"""
[file x/__init__.py]
i: int = 0
[file x/_skipme_please.py]
This isn't even syntatically valid!
[file x/please_skipme_.py]
Neither is this!

[case testSequenceExcludePyprojectTOML]
# cmd: mypy x
[file pyproject.toml]
\[tool.mypy]
exclude = [
"(^|/)[^/]*skipme_\\.py$",
"(^|/)_skipme[^/]*\\.py$",
]
[file x/__init__.py]
i: int = 0
[file x/_skipme_please.py]
This isn't even syntatically valid!
[file x/please_skipme_.py]
Neither is this!

0 comments on commit 435c1a9

Please sign in to comment.