From d6bd841512e1a5fc37b75a21e0b2239c5b0bc145 Mon Sep 17 00:00:00 2001 From: Auguste Lalande Date: Tue, 10 Sep 2024 13:25:38 -0400 Subject: [PATCH] [`pydoclint`] Ignore `DOC201` when function name is "__new__" (#13300) --- .../resources/test/fixtures/pydoclint/DOC201_google.py | 7 +++++++ .../src/rules/pydoclint/rules/check_docstring.rs | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC201_google.py b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC201_google.py index d2088336db..a0b4f93871 100644 --- a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC201_google.py +++ b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC201_google.py @@ -207,3 +207,10 @@ def foo(s: str) -> str | None: s (str): A string. """ return None + + +class Spam: + # OK + def __new__(cls) -> 'Spam': + """New!!""" + return cls() diff --git a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs index 40d8802303..1f75dbcb4b 100644 --- a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs +++ b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs @@ -741,6 +741,10 @@ fn returns_documented( || (matches!(convention, Some(Convention::Google)) && starts_with_returns(docstring)) } +fn should_document_returns(function_def: &ast::StmtFunctionDef) -> bool { + !matches!(function_def.name.as_str(), "__new__") +} + fn starts_with_yields(docstring: &Docstring) -> bool { if let Some(first_word) = docstring.body().as_str().split(' ').next() { return matches!(first_word, "Yield" | "Yields"); @@ -868,7 +872,9 @@ pub(crate) fn check_docstring( // DOC201 if checker.enabled(Rule::DocstringMissingReturns) { - if !returns_documented(docstring, &docstring_sections, convention) { + if should_document_returns(function_def) + && !returns_documented(docstring, &docstring_sections, convention) + { let extra_property_decorators = checker.settings.pydocstyle.property_decorators(); if !definition.is_property(extra_property_decorators, semantic) { if let Some(body_return) = body_entries.returns.first() {