From 8dee619363f1080419178eb76cb16f267128847c Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Thu, 30 Dec 2021 14:39:25 -0600 Subject: [PATCH] Add D419: Add and switch to "Docstring is empty" error code (#559) Co-authored-by: Sambhav Kothari --- docs/release_notes.rst | 1 + src/pydocstyle/checker.py | 19 +++++++++++++------ src/pydocstyle/violations.py | 4 ++++ src/tests/test_cases/capitalization.py | 2 +- src/tests/test_cases/test.py | 6 +++--- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 72332114..b70aca01 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -12,6 +12,7 @@ New Features * Add support for `property_decorators` config to ignore D401. * Add support for Python 3.10 (#554). +* Replace D10X errors with D419 if docstring exists but is empty (#559). 6.1.1 - May 17th, 2021 --------------------------- diff --git a/src/pydocstyle/checker.py b/src/pydocstyle/checker.py index be74867f..5c5522e7 100644 --- a/src/pydocstyle/checker.py +++ b/src/pydocstyle/checker.py @@ -196,12 +196,7 @@ def check_docstring_missing(self, definition, docstring): with a single underscore. """ - if ( - not docstring - and definition.is_public - or docstring - and is_blank(ast.literal_eval(docstring)) - ): + if not docstring and definition.is_public: codes = { Module: violations.D100, Class: violations.D101, @@ -227,6 +222,18 @@ def check_docstring_missing(self, definition, docstring): } return codes[type(definition)]() + @check_for(Definition, terminal=True) + def check_docstring_empty(self, definition, docstring): + """D419: Docstring is empty. + + If the user provided a docstring but it was empty, it is like they never provided one. + + NOTE: This used to report as D10X errors. + + """ + if docstring and is_blank(ast.literal_eval(docstring)): + return violations.D419() + @check_for(Definition) def check_one_liners(self, definition, docstring): """D200: One-liner docstrings should fit on one line with quotes. diff --git a/src/pydocstyle/violations.py b/src/pydocstyle/violations.py index 60fc064e..8156921a 100644 --- a/src/pydocstyle/violations.py +++ b/src/pydocstyle/violations.py @@ -415,6 +415,10 @@ def to_rst(cls) -> str: 'D418', 'Function/ Method decorated with @overload shouldn\'t contain a docstring', ) +D419 = D4xx.create_error( + 'D419', + 'Docstring is empty', +) class AttrDict(dict): diff --git a/src/tests/test_cases/capitalization.py b/src/tests/test_cases/capitalization.py index 91ecf45c..a15c79f3 100644 --- a/src/tests/test_cases/capitalization.py +++ b/src/tests/test_cases/capitalization.py @@ -13,7 +13,7 @@ def not_capitalized(): # Make sure empty docstrings don't generate capitalization errors. -@expect("D103: Missing docstring in public function") +@expect("D419: Docstring is empty") def empty_docstring(): """""" diff --git a/src/tests/test_cases/test.py b/src/tests/test_cases/test.py index 8154c960..1cbd8ec4 100644 --- a/src/tests/test_cases/test.py +++ b/src/tests/test_cases/test.py @@ -13,7 +13,7 @@ class class_: - expect('meta', 'D106: Missing docstring in public nested class') + expect('meta', 'D419: Docstring is empty') class meta: """""" @@ -64,13 +64,13 @@ def __call__(self=None, x=None, y=None, z=None): pass -@expect('D103: Missing docstring in public function') +@expect('D419: Docstring is empty') def function(): """ """ def ok_since_nested(): pass - @expect('D103: Missing docstring in public function') + @expect('D419: Docstring is empty') def nested(): ''