jsh9 / pydoclint

A Python docstring linter that checks arguments, returns, yields, and raises sections
https://pypi.org/project/pydoclint/
MIT License
149 stars 15 forks source link

False positives for DOC503? #165

Closed sondrfos closed 2 months ago

sondrfos commented 2 months ago

Hello,

Thank you for maintaining the great tool pydoclint! We were about to merge 0.5.7 to our project, but it seems as though the newly added rule DOC503 results in some false positives.

Let me try to give a minimal example:

class CredentialsValidationError(Exception):
    pass

This exception-definition is located in a file called exceptions.py

Then another file imports the entire exceptions.py and uses the CredentialsValidationError-exception from this file

import exceptions

def foo():
    """Bar.

    Raises:
        exceptions.CredentialsValidationError: An exception
    """
    raise exceptions.CredentialsValidationError

This results in a DOC503-error with the message

DOC503: Function `foo` exceptions in the "Raises" section in the docstring do not match those in the function body Raises values in the docstring: ['exceptions.CredentialsValidationError']. Raised exceptions in the body: ['exceptions'].

Is this intended behaviour? Is there somthing here I dont understand? Thanks!

jsh9 commented 2 months ago

Hi @sondrfos : thank you for finding this issue!

Hi @Amar1729 , I was able to reproduce this issue, and I found that it occurred in these lines:

https://github.com/jsh9/pydoclint/blob/f75860489c4d4769767beabcad9b099cc2b2a88d/pydoclint/utils/return_yield_raise.py#L133-L136

Using the example above, subnode.id is exceptions, and child.exc.attr is CredentialsValidationError.

Do you have a simply and quick way to fix this? Thanks!

Amar1729 commented 2 months ago

I do love more edge cases 😉

I'll take a look!

jonyscathe commented 2 months ago

Any update on this? Am getting the same sort of error on a raise httpx.ConnectError (raised in a test fixture):

exceptions in the "Raises" section in the docstring do not match those in the function body Raises values in the docstring: ['ConnectError']. Raised exceptions in the body: ['httpx']

I can obviously get around this by importing ConnectError from httpx separately to the rest of the package, but not ideal to have to do so.

jsh9 commented 2 months ago

I'm working on a potential solution now, and I'll create a PR soon (in a few days).

Amar1729 commented 2 months ago

hey @jsh9 sorry for the delay on this. work got a little crazy and i forgot about this notif. just pushed up a change

Apakottur commented 2 months ago

I'm encountering what I think is the same issue but am not sure:

# test.py
import test2

def func() -> None:
    """
    text

    Raises:
        test2.MyException: exception
    """

    try:
        x = 1
    except test2.MyException:
        raise
# test2.py
class MyException(Exception):
    pass

When running pydoclint I get:

test.py:4:1: DOC503 Function `func` exceptions in the "Raises" section in the docstring do not match those in the function body Raises values in the docstring: ['test2.MyException']. Raised exceptions in the body: [].
Amar1729 commented 2 months ago

@Apakottur that is the same issue, thanks for reporting. Looks like that situation isn't caught by my PR; working on an update updated.

jsh9 commented 2 months ago

Published 0.5.8 on PyPI.