From 1d010d093ae2b141855e00119136b50bcadc2692 Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Fri, 31 Dec 2021 19:45:14 -0600 Subject: [PATCH 1/3] Revert to treating exclude in .ini as single string Partially reverts #11329 (with respect to `.ini` documentation). Modifies existing unit tests. Fixes #11830. --- docs/source/config_file.rst | 9 +++++---- mypy/config_parser.py | 2 +- test-data/unit/cmdline.test | 7 ++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index c34f23d9e169..28548398cff9 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -197,18 +197,19 @@ section of the command line docs. .. confval:: exclude - :type: newline separated list of regular expressions + :type: regular expressions - A newline list of regular expression that matches file names, directory names and paths + A regular expression that matches file names, directory names and paths which mypy should ignore while recursively discovering files to check. Use forward slashes on all platforms. .. code-block:: ini [mypy] - exclude = + exclude = (?x)( ^file1\.py$ - ^file2\.py$ + |^file2\.py$ + ) For more details, see :option:`--exclude `. diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 24e61df0441c..f637e7d0ad14 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -126,7 +126,7 @@ def check_follow_imports(choice: str) -> str: 'cache_dir': expand_path, 'python_executable': expand_path, 'strict': bool, - 'exclude': lambda s: [p.strip() for p in s.split('\n') if p.strip()], + 'exclude': lambda s: [s.strip()], } # Reuse the ini_config_types and overwrite the diff diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 6fa4d210a826..690b0cc06709 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -1354,9 +1354,10 @@ b/bpkg.py:1: error: "int" not callable # cmd: mypy . [file mypy.ini] \[mypy] -exclude = - abc - b +exclude = (?x)( + ^abc/ + |^b/ + ) [file abc/apkg.py] 1() [file b/bpkg.py] From c197a70dd2c10c8c75ba96d38a3e666be19b9ab2 Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Sat, 1 Jan 2022 18:26:23 -0600 Subject: [PATCH 2/3] Update docs/source/config_file.rst Co-authored-by: Matthew W --- docs/source/config_file.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 28548398cff9..9a278b23327d 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -197,7 +197,7 @@ section of the command line docs. .. confval:: exclude - :type: regular expressions + :type: regular expression A regular expression that matches file names, directory names and paths which mypy should ignore while recursively discovering files to check. From 6cb514d962f6e52443afb46ee7ac2fb7d4d2c177 Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Sat, 1 Jan 2022 19:06:18 -0600 Subject: [PATCH 3/3] Proposal to address reviewer feedback Add explanation of regex in ``.ini`` example per @mawillcockson's suggestion. --- docs/source/config_file.rst | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 9a278b23327d..889d6ebfa1b4 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -207,10 +207,19 @@ section of the command line docs. [mypy] exclude = (?x)( - ^file1\.py$ - |^file2\.py$ + ^one\.py$ # files named "one.py" + | two\.pyi$ # or files ending with "two.pyi" + | ^three\. # or files starting with "three." ) + Crafting a single regular expression that excludes multiple files while remaining + human-readable can be a challenge. The above example demonstrates one approach. + ``(?x)`` enables the ``VERBOSE`` flag for the subsequent regular expression, which + `ignores most whitespace and supports comments`__. The above is equivalent to: + ``(^one\.py$|two\.pyi$|^three\.)``. + + .. __: https://docs.python.org/3/library/re.html#re.X + For more details, see :option:`--exclude `. This option may only be set in the global section (``[mypy]``).