Skip to content

DOC502 false positive on raise e #20973

@dscorbett

Description

@dscorbett

Summary

docstring-extraneous-exception (DOC502) reports a false positive when an exception mentioned in the docstring is caught and explicitly reraised (i.e. raise e or raise e from None, assuming e is bound in the except clause and not rebound before it is raised). It correctly ignores implicit reraising (i.e. just raise) of a documented exception. The upstream implementation in pydoclint gets it right both ways. Example:

$ cat >doc502.py <<'# EOF'
def f():
    """Do nothing.

    Raises
    ------
    OSError
        If the OS errors.
    """
    try:
        pass
    except OSError as e:
        raise e
# EOF

$ ruff --isolated check doc502.py --select DOC502 --preview --output-format concise -q
doc502.py:2:5: DOC502 Raised exception is not explicitly raised: `OSError`

$ pydoclint doc502.py
Skipping files that match this pattern: \.git|\.tox
doc502.py
🎉 No violations 🎉

Version

ruff 0.14.1 (2bffef5 2025-10-16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpreviewRelated to preview mode featuresruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions