[pydoclint] Fix false positive for exceptions raised via classmethods and chained calls (DOC501, DOC502)#23637
Open
bxff wants to merge 2 commits intoastral-sh:mainfrom
Open
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| DOC501 | 59 | 30 | 29 | 0 | 0 |
| DOC502 | 8 | 4 | 4 | 0 | 0 |
398651f to
cd49412
Compare
pydoclint] Fix false positive for exception raised via classmethod (DOC501, DOC502)pydoclint] Fix false positive for exceptions raised via classmethods and chained calls (DOC501, DOC502)
…ds and chained calls (`DOC501`, `DOC502`) When an exception is raised via a classmethod, factory method, or chained method call (e.g., `raise ValueError.from_exception_data(...)` or `raise Error.create(...).with_traceback(tb)`), the rule incorrectly extracted the method name as the exception type instead of the class name. The fix replaces the simple `map_callable` + `resolve_qualified_name` approach with a loop that recursively unwraps method chain calls (Call+Attribute layers) until it reaches a resolvable name. This correctly handles: - `raise Error(...)` → resolves `Error` - `raise Error.from_data(...)` → resolves `Error` - `raise Error.create(...).with_traceback(tb)` → resolves `Error` - `raise module.Error` → resolves `module.Error` (bare attribute, not unwrapped)
cd49412 to
d8b50f5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #23570.
raise ValueError.from_exception_data(...)causes DOC501 to reportfrom_exception_dataas the missing exception instead ofValueError, and DOC502 to flagValidationErroras not raised. The same happens with chained calls likeraise Error.create(...).with_traceback(tb).The issue is that
map_callableunwraps the call, leaving anExpr::Attribute(ValueError.from_exception_data), andresolve_qualified_namethen resolves the method name rather than the class.This replaces the
map_callable+resolve_qualified_nameone-shot with a loop that peels off Call+Attribute layers until it hits a resolvable name. Bare attribute raises likeraise module.SomeErrorare unaffected since they skip the loop entirely.Test Plan
Added test cases to
DOC501_google.pyfor classmethod raises (documented + undocumented), imported exceptions via classmethod, and chained.with_traceback()calls. All 18 pydoclint tests pass, no regressions on existingraise something.SomeErrortests.