terrencepreilly / darglint

A python documentation linter which checks that the docstring description matches the definition.
MIT License
482 stars 41 forks source link

DAR402 wrongly reported on bare raise #134

Closed fredrikaverpil closed 4 years ago

fredrikaverpil commented 4 years ago

With this code, I get Excess exception(s) in Raises section: +r TypeErrorflake8(DAR402) with darglint 1.5.5 via flake8:

"""Test."""

from somewhere import y, z

def x():
    """Do stuff.

    Raises:
        TypeError: wrong type
    """
    try:
        y()
    except TypeError as err:
        z(err)
        raise

I feel this is wrong, as I am inside of x() indeed re-raising TypeError.

Related:

fredrikaverpil commented 4 years ago

@terrencepreilly hi, I was just wondering if you agree with me that the above reported issue - or if this is a misunderstanding on my part.

terrencepreilly commented 4 years ago

Yes, confirmed that this is handled incorrectly. I just haven't had time to handle this yet. I'll give it a look this weekend.

terrencepreilly commented 4 years ago

This should be resolved in b33b261e43bd31383efe71f7b9295cdf58c9857b, and the fix will be present in v1.5.6, which I'll try to push this weekend.

terrencepreilly commented 4 years ago

It should now be available.

fredrikaverpil commented 4 years ago

Hi @terrencepreilly I am noticing that with v1.5.7, a bare return now requires a "Returns:" statement in the docstring. Was this a side-effect of allowing bare raising?

If a function never returns anything (always returns None) but you want the function to exit early, it's common practice to do a "bare return". Would you say the intent here is to require a "Returns:" statement in the dosctring in such cases?

fredrikaverpil commented 4 years ago

Ah, I just saw your comment on that this should be fixed in 1.5.8 👍

fredrikaverpil commented 4 years ago

Hi again @terrencepreilly

What about this scenario? With darglint 1.5.8, I still get "excessive exceptions in Raises section" (DAR402) for both HTTPError and ConnectionError in the example below:

"""Some script."""

import requests

def something():
    """Something."""
    pass

def test_something():
    """Test.

    Raises:
        HTTPError: if call to something fails...
        ConnectionError: if call to something fails...

    """
    try:
        something()
    except (requests.HTTPError, requests.ConnectionError) as err:
        print(err)
        raise
stephen-dexda commented 4 years ago

Similarly, as of 1.5.8:

def foo():
    """Try bar() 5 times.

    Raises:
        Exception: if bar() excepts 5 times.

    """
    for _try in range(5):
        try:
            bar()
        except Exception as exc:
            last_exc = exc
    raise last_exc
DAR401: Missing exception(s) in Raises section: -r last_exc
DAR402: Excess exception(s) in Raises section: +r Exception