From bd4993345a241bd0d5128f21de56394b1cde3714 Mon Sep 17 00:00:00 2001 From: oczkoisse Date: Thu, 30 Dec 2021 13:16:07 -0800 Subject: [PATCH] Fix match option to only consider basename when given a path argument (#550) Co-authored-by: Sambhav Kothari --- docs/release_notes.rst | 4 ++++ src/pydocstyle/config.py | 4 ++-- src/tests/test_integration.py | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 5da4b27d..a9060aef 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -15,6 +15,10 @@ New Features * Add support for Python 3.10 (#554). * Replace D10X errors with D419 if docstring exists but is empty (#559). +Bug Fixes + +* Fix ``--match`` option to only consider filename when matching full paths (#550). + 6.1.1 - May 17th, 2021 --------------------------- diff --git a/src/pydocstyle/config.py b/src/pydocstyle/config.py index fe3afb3f..ed00c874 100644 --- a/src/pydocstyle/config.py +++ b/src/pydocstyle/config.py @@ -288,7 +288,7 @@ def _get_property_decorators(conf): # Skip any dirs that do not match match_dir dirs[:] = [d for d in dirs if match_dir(d)] - for filename in filenames: + for filename in map(os.path.basename, filenames): if match(filename): full_path = os.path.join(root, filename) yield ( @@ -302,7 +302,7 @@ def _get_property_decorators(conf): match, _ = _get_matches(config) ignore_decorators = _get_ignore_decorators(config) property_decorators = _get_property_decorators(config) - if match(name): + if match(os.path.basename(name)): yield ( name, list(config.checked_codes), diff --git a/src/tests/test_integration.py b/src/tests/test_integration.py index 7e399cce..9f255626 100644 --- a/src/tests/test_integration.py +++ b/src/tests/test_integration.py @@ -1489,3 +1489,25 @@ def test_comment_with_noqa_plus_docstring_file(env): out, _, code = env.invoke() assert '' == out assert code == 0 + + +def test_match_considers_basenames_for_path_args(env): + """Test that `match` option only considers basenames for path arguments. + + The test environment consists of a single empty module `test_a.py`. The + match option is set to a pattern that ignores test_ prefixed .py filenames. + When pydocstyle is invoked with full path to `test_a.py`, we expect it to + succeed since match option will match against just the file name and not + full path. + """ + # Ignore .py files prefixed with 'test_' + env.write_config(select='D100', match='(?!test_).+.py') + + # Create an empty module (violates D100) + with env.open('test_a.py', 'wt') as test: + test.write('') + + # env.invoke calls pydocstyle with full path to test_a.py + out, _, code = env.invoke(target='test_a.py') + assert '' == out + assert code == 0