diff --git a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC402_google.py b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC402_google.py new file mode 100644 index 0000000000000..3c286bf697b9a --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC402_google.py @@ -0,0 +1,68 @@ +# DOC402 +def foo(num: int) -> str: + """ + Do something + + Args: + num (int): A number + """ + yield 'test' + + +# OK +def foo(num: int) -> str: + """ + Do something + + Args: + num (int): A number + + Yields: + str: A string + """ + yield 'test' + + +class Bar: + + # OK + def foo(self) -> str: + """ + Do something + + Args: + num (int): A number + + Yields: + str: A string + """ + yield 'test' + + + # DOC402 + def bar(self) -> str: + """ + Do something + + Args: + num (int): A number + """ + yield 'test' + + +# OK +def test(): + """Do something.""" + # DOC402 + def nested(): + """Do something nested.""" + yield 5 + + print("I never yield") + + +# DOC402 +def test(): + """Do something.""" + yield from range(10) + diff --git a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC402_numpy.py b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC402_numpy.py new file mode 100644 index 0000000000000..bde7a2afdea8d --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC402_numpy.py @@ -0,0 +1,62 @@ +# DOC402 +def foo(num: int) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + """ + yield 'test' + + +# OK +def foo(num: int) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + + Yields + ------- + str + A string + """ + yield 'test' + + +class Bar: + + # OK + def foo(self) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + + Yields + ------- + str + A string + """ + yield 'test' + + + # DOC402 + def bar(self) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + """ + yield 'test' diff --git a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC403_google.py b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC403_google.py new file mode 100644 index 0000000000000..70c9c53112eef --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC403_google.py @@ -0,0 +1,50 @@ +# OK +def foo(num: int) -> str: + """ + Do something + + Args: + num (int): A number + """ + print('test') + + +# DOC403 +def foo(num: int) -> str: + """ + Do something + + Args: + num (int): A number + + Yields: + str: A string + """ + print('test') + + +class Bar: + + # DOC403 + def foo(self) -> str: + """ + Do something + + Args: + num (int): A number + + Yields: + str: A string + """ + print('test') + + + # OK + def bar(self) -> str: + """ + Do something + + Args: + num (int): A number + """ + print('test') diff --git a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC403_numpy.py b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC403_numpy.py new file mode 100644 index 0000000000000..5d5c646a90f2f --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC403_numpy.py @@ -0,0 +1,62 @@ +# OK +def foo(num: int) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + """ + print('test') + + +# DOC403 +def foo(num: int) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + + Yields + ------- + str + A string + """ + print('test') + + +class Bar: + + # DOC403 + def foo(self) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + + Yields + ------- + str + A string + """ + print('test') + + + # OK + def bar(self) -> str: + """ + Do something + + Parameters + ---------- + num : int + A number + """ + print('test') diff --git a/crates/ruff_linter/src/rules/pydoclint/mod.rs b/crates/ruff_linter/src/rules/pydoclint/mod.rs index 77399017677e9..db2c79662f685 100644 --- a/crates/ruff_linter/src/rules/pydoclint/mod.rs +++ b/crates/ruff_linter/src/rules/pydoclint/mod.rs @@ -28,6 +28,8 @@ mod tests { #[test_case(Rule::DocstringMissingReturns, Path::new("DOC201_google.py"))] #[test_case(Rule::DocstringExtraneousReturns, Path::new("DOC202_google.py"))] + #[test_case(Rule::DocstringMissingYields, Path::new("DOC402_google.py"))] + #[test_case(Rule::DocstringExtraneousYields, Path::new("DOC403_google.py"))] #[test_case(Rule::DocstringMissingException, Path::new("DOC501_google.py"))] #[test_case(Rule::DocstringExtraneousException, Path::new("DOC502_google.py"))] fn rules_google_style(rule_code: Rule, path: &Path) -> Result<()> { @@ -49,6 +51,8 @@ mod tests { #[test_case(Rule::DocstringMissingReturns, Path::new("DOC201_numpy.py"))] #[test_case(Rule::DocstringExtraneousReturns, Path::new("DOC202_numpy.py"))] + #[test_case(Rule::DocstringMissingYields, Path::new("DOC402_numpy.py"))] + #[test_case(Rule::DocstringExtraneousYields, Path::new("DOC403_numpy.py"))] #[test_case(Rule::DocstringMissingException, Path::new("DOC501_numpy.py"))] #[test_case(Rule::DocstringExtraneousException, Path::new("DOC502_numpy.py"))] fn rules_numpy_style(rule_code: Rule, path: &Path) -> Result<()> { diff --git a/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-extraneous-yields_DOC403_google.py.snap b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-extraneous-yields_DOC403_google.py.snap new file mode 100644 index 0000000000000..3ee75598a189e --- /dev/null +++ b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-extraneous-yields_DOC403_google.py.snap @@ -0,0 +1,24 @@ +--- +source: crates/ruff_linter/src/rules/pydoclint/mod.rs +--- +DOC403_google.py:20:1: DOC403 Docstring should not have a yields section because the function doesn't yield anything + | +18 | num (int): A number +19 | +20 | / Yields: +21 | | str: A string +22 | | """ + | |____^ DOC403 +23 | print('test') + | + +DOC403_google.py:36:1: DOC403 Docstring should not have a yields section because the function doesn't yield anything + | +34 | num (int): A number +35 | +36 | / Yields: +37 | | str: A string +38 | | """ + | |________^ DOC403 +39 | print('test') + | diff --git a/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-extraneous-yields_DOC403_numpy.py.snap b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-extraneous-yields_DOC403_numpy.py.snap new file mode 100644 index 0000000000000..394f7c7ca38df --- /dev/null +++ b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-extraneous-yields_DOC403_numpy.py.snap @@ -0,0 +1,28 @@ +--- +source: crates/ruff_linter/src/rules/pydoclint/mod.rs +--- +DOC403_numpy.py:24:1: DOC403 Docstring should not have a yields section because the function doesn't yield anything + | +22 | A number +23 | +24 | / Yields +25 | | ------- +26 | | str +27 | | A string +28 | | """ + | |____^ DOC403 +29 | print('test') + | + +DOC403_numpy.py:44:1: DOC403 Docstring should not have a yields section because the function doesn't yield anything + | +42 | A number +43 | +44 | / Yields +45 | | ------- +46 | | str +47 | | A string +48 | | """ + | |________^ DOC403 +49 | print('test') + | diff --git a/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-yields_DOC402_google.py.snap b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-yields_DOC402_google.py.snap new file mode 100644 index 0000000000000..8226ed16ff95e --- /dev/null +++ b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-yields_DOC402_google.py.snap @@ -0,0 +1,36 @@ +--- +source: crates/ruff_linter/src/rules/pydoclint/mod.rs +--- +DOC402_google.py:9:5: DOC402 `yield` is not documented in docstring + | +7 | num (int): A number +8 | """ +9 | yield 'test' + | ^^^^^^^^^^^^ DOC402 + | + +DOC402_google.py:50:9: DOC402 `yield` is not documented in docstring + | +48 | num (int): A number +49 | """ +50 | yield 'test' + | ^^^^^^^^^^^^ DOC402 + | + +DOC402_google.py:59:9: DOC402 `yield` is not documented in docstring + | +57 | def nested(): +58 | """Do something nested.""" +59 | yield 5 + | ^^^^^^^ DOC402 +60 | +61 | print("I never yield") + | + +DOC402_google.py:67:5: DOC402 `yield` is not documented in docstring + | +65 | def test(): +66 | """Do something.""" +67 | yield from range(10) + | ^^^^^^^^^^^^^^^^^^^^ DOC402 + | diff --git a/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-yields_DOC402_numpy.py.snap b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-yields_DOC402_numpy.py.snap new file mode 100644 index 0000000000000..55233c7db09c0 --- /dev/null +++ b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-yields_DOC402_numpy.py.snap @@ -0,0 +1,18 @@ +--- +source: crates/ruff_linter/src/rules/pydoclint/mod.rs +--- +DOC402_numpy.py:11:5: DOC402 `yield` is not documented in docstring + | + 9 | A number +10 | """ +11 | yield 'test' + | ^^^^^^^^^^^^ DOC402 + | + +DOC402_numpy.py:62:9: DOC402 `yield` is not documented in docstring + | +60 | A number +61 | """ +62 | yield 'test' + | ^^^^^^^^^^^^ DOC402 + |