Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC501 False Positive #12647

Closed
will-schneble opened this issue Aug 2, 2024 · 8 comments · Fixed by #12649
Closed

DOC501 False Positive #12647

will-schneble opened this issue Aug 2, 2024 · 8 comments · Fixed by #12649
Assignees
Labels
bug Something isn't working docstring Related to docstring linting or formatting

Comments

@will-schneble
Copy link

Describe the bug
DOC501 rule violation when the docstring includes the exception.

ruff==0.5.5
python==3.12.4

Command: ruff check --preview --select DOC

No pyproject.toml or other configuration than what's shown here.

Steps to reproduce
Sample file to test against.

class Foo:
    def __init__(self, bar, zap):
        self.bar = bar
        self.zap = zap
        
    def get_bar(self) -> str:
        """Print and return bar.
        
        Raises:
            ValueError: bar is not bar.

        Returns:
            str: bar value.
        """    
        print(self.bar)
        if self.bar != "bar":
            raise ValueError(self.bar)
        return self.bar

Expected behavior
No violations.

Running pydoclint --style=google test.py gives the expected no violation result.

Debug logs

test.py:17:19: DOC501 Raised exception `ValueError` missing from docstring
   |
15 |         print(self.bar)
16 |         if self.bar != "bar":
17 |             raise ValueError(self.bar)
   |                   ^^^^^^^^^^^^^^^^^^^^ DOC501
18 |         return self.bar
   |

Notes
Adding an argument to Foo.get_bar and adding that to the docstring then makes the DOC501 violation go away. Example below:

    def get_bar(self, x: str) -> str:
        """Print and return bar.
        
        Args:
            x (str): A dummy value.
        
        Raises:
            ValueError: bar is not bar.

        Returns:
            str: bar value
        """    
        print(self.bar)
        if self.bar != "bar":
            raise ValueError(self.bar)
        return self.bar
@will-schneble
Copy link
Author

Just saw ruff==0.5.6 released. Double checked and it is still an issue in that version.

@charliermarsh charliermarsh added bug Something isn't working docstring Related to docstring linting or formatting labels Aug 2, 2024
@charliermarsh
Copy link
Member

Thank you!

@charliermarsh
Copy link
Member

I think the issue is that the above section is actually ambiguous. All the defined sections are valid section names in both NumPy and Google. You should set the convention explicitly in your pyproject.toml, e.g.:

[tool.ruff.lint.pydocstyle]
convention = "google"

(Adding Args works because Args is Google-only, so we can safely differentiate.)

@will-schneble
Copy link
Author

You're correct, adding --config 'lint.pydocstyle.convention="google"' produces the expected results. For some reason I thought pydocstyle config didn't affect the pydoclint rules. Thanks for looking into it.

@JasonGrace2282
Copy link

It might make sense to produce a more reasonable error message instead of a vague DOC501 linting error?

@charliermarsh
Copy link
Member

@JasonGrace2282 - can you expand on that? It’s inferring a NumPy docstring, and in that case, it can’t find any raises.

@will-schneble - gonna leave this open because I think we can improve our detection.

@JasonGrace2282
Copy link

JasonGrace2282 commented Aug 2, 2024

I'm not familiar with how the parsing works in Ruff, but as a user I would expect a warning if the convention is numpy but a docstring looks like Google convention.
I understand what I just said is a bit vague (and possibly hard to implement), so really any kind of hint if the docstring convention isn't explicitly set would be useful imo.

@trim21
Copy link
Contributor

trim21 commented Aug 8, 2024

this is confusing, doc use google style but it requires extra config

https://docs.astral.sh/ruff/rules/docstring-missing-exception/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docstring Related to docstring linting or formatting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants