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

W9006: missing-raises-doc false positive when declaring multiple exceptions in SphinxDocstring #2729

Closed
StephenBrown2 opened this issue Feb 6, 2019 · 8 comments
Labels
Bug 🪲 Good first issue Friendly and approachable by new contributors

Comments

@StephenBrown2
Copy link

StephenBrown2 commented Feb 6, 2019

Steps to reproduce

Using load-plugins=pylint.extensions.docparams

  1. Create a function which potentially raises multiple Exceptions:
def function_name(args):
    if isinstance(args, dict):
        raise TypeError
    elif isinstance(args, str):
        raise ValueError
    else:
        raise ValidationError
  1. Document such functionality as :raises TypeError, ValueError, ValidationError
    """ A function that raises exceptions

    :param args: The arguments
    :type args: int

    :raises TypeError, ValueError, ValidationError:
    """
  1. Get [W9006(missing-raises-doc), function_name] "ValueError" not documented as being raised, et al.

Full test file:

#!/usr/bin/env python3

class ValidationError(Exception):
    pass

def function_name(args):
    """ A function that raises exceptions

    :param args: The arguments
    :type args: int

    :raises TypeError, ValueError, ValidationError:
    """
    if isinstance(args, dict):
        raise TypeError
    elif isinstance(args, str):
        raise ValueError
    else:
        raise ValidationError

Current behavior

************* Module test_raises
test_raises.py:6: [W9006(missing-raises-doc), function_name] "TypeError" not documented as being raised
test_raises.py:6: [W9006(missing-raises-doc), function_name] "ValueError" not documented as being raised
test_raises.py:6: [W9006(missing-raises-doc), function_name] "ValidationError" not documented as being raised

Expected behavior

pylint passes / Your code has been rated at 10.00/10

pylint --version output

$ pylint --version
pylint 2.2.2
astroid 2.1.0
Python 3.7.2 (default, Jan 10 2019, 23:51:51)
[GCC 8.2.1 20181127]

Notes

Sphinx properly parses the Exception types raised and links them individually, so I believe only a small change would be needed in the regex here and parsing of such here to allow for comma+whitespace (, *) separated types.

@PCManticore
Copy link
Contributor

Thanks for reporting the issue @StephenBrown2 This could definitely be supported.

@PCManticore PCManticore added Bug 🪲 Good first issue Friendly and approachable by new contributors labels Feb 11, 2019
@gaurikholkar-zz
Copy link
Contributor

Hi, I'd like to take this up!

@PCManticore
Copy link
Contributor

Hey @gaurikholkar Sure thing! @StephenBrown2 left some indications regarding what needs to be modified and where. Let me know if there's anything else you might need before tackling this issue.

@gaurikholkar-zz
Copy link
Contributor

hi @PCManticore, got a little busy with work. Will try to get this done in a day or two!

@sebastianmika
Copy link

Hi - any update on this really slightly annoying issue @gaurikholkar? If not I would spend some time trying to fix it - it is in my way :-)

@gaurikholkar-zz
Copy link
Contributor

@sebastianmika giving it a try now

@gaurikholkar-zz
Copy link
Contributor

@StephenBrown2, I'm unsure about the regex pattern here. Could you maybe give me pointers regarding the same?

Should the expression here

re_raise_raw = r"""
        :                       # initial colon
        (?:                     # Sphinx keyword
        raises?|
        except|exception
        )
        \s+                     # whitespace
        (?:                     # type declaration
        ({type})
        \s+
        )?
        (\w+)                   # Parameter name
        \s*                     # whitespace
        :                       # final colon
        """.format(
        type=re_type

be

re_raise_raw = r"""
        :                       # initial colon
        (?:                     # Sphinx keyword
        raises?|
        except|exception
        )
        \s+                     # whitespace
        (?:                     # type declaration
        ({type},\s})+|({type})
        \s+
        )?
        (\w+)                   # Parameter name
        \s*                     # whitespace
        :                       # final colon
        """.format(
        type=re_type

@StephenBrown2
Copy link
Author

That's about where I got stuck too. I believe that would probably work or at least be a good start. I would say to try by writing a test case using the Steps to Reproduce in the OP, and tweak until it passes. Sorry I can't be of more help with that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Good first issue Friendly and approachable by new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants