From 3b4dcb52fd1b48762a06a9e6af45f71bcb32e1d6 Mon Sep 17 00:00:00 2001 From: Amar Paul Date: Sat, 14 Dec 2024 16:17:51 -0500 Subject: [PATCH] fix: get name of a raised exception when bound in except (#184) Co-authored-by: jsh9 <25124332+jsh9@users.noreply.github.com> --- CHANGELOG.md | 14 ++++++++++++++ pydoclint/utils/return_yield_raise.py | 15 +++++++++++++++ setup.cfg | 2 +- tests/utils/test_returns_yields_raise.py | 11 +++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7e2f58..b3e1ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Change Log +## [0.5.11] - 2024-12-14 + +- Fixed + + - Fixed a bug where pydoclint uses variable names instead of the exception + itself (https://github.com/jsh9/pydoclint/issues/175) + +- Full diff + - https://github.com/jsh9/pydoclint/compare/0.5.11...0.5.10 + ## [0.5.10] - 2024-12-07 - Changed @@ -9,9 +19,13 @@ - Fixed a bug where assigning a value to an attribute caused pydoclint to crash - Changed + - Renamed function `unparseAnnotation()` into `unparseNode()` - Renamed `EdgeCaseError` into `EdgeCaseError` +- Full diff + - https://github.com/jsh9/pydoclint/compare/0.5.9...0.5.10 + ## [0.5.9] - 2024-09-29 - Fixed diff --git a/pydoclint/utils/return_yield_raise.py b/pydoclint/utils/return_yield_raise.py index 4786a76..dde2525 100644 --- a/pydoclint/utils/return_yield_raise.py +++ b/pydoclint/utils/return_yield_raise.py @@ -140,6 +140,21 @@ def _getRaisedExceptions( ): # case: looks like m.n.exception() yield getFullAttributeName(child.exc.func) + elif ( + currentParentExceptHandler + and currentParentExceptHandler.name + and subnode.id == currentParentExceptHandler.name + ): + # case: "except <> as e; raise e" -> we must yield the + # stuff in <> + # + # Note: if subnode.id != currentParentExceptHandler.name, + # the user is raising something not bound by + # this exception handler (meaning we should fall + # through to yielding the subnode.id) + yield from _extractExceptionsFromExcept( + currentParentExceptHandler + ) else: yield subnode.id diff --git a/setup.cfg b/setup.cfg index 4d5d763..9c51a55 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pydoclint -version = 0.5.10 +version = 0.5.11 description = A Python docstring linter that checks arguments, returns, yields, and raises sections long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/utils/test_returns_yields_raise.py b/tests/utils/test_returns_yields_raise.py index 7b63310..6ab18a3 100644 --- a/tests/utils/test_returns_yields_raise.py +++ b/tests/utils/test_returns_yields_raise.py @@ -440,6 +440,15 @@ def func15(): x = 1 except other.Exception: raise + +def func16(): + # ensure that "as e" doesn't mess up getting the name of an exception. + try: + 3 + 3 + except IOError as e: + raise e + except (KeyError, IndexError) as e: + raise e """ @@ -466,6 +475,7 @@ def testHasRaiseStatements() -> None: (117, 0, 'func13'): True, (126, 0, 'func14'): True, (135, 0, 'func15'): True, + (141, 0, 'func16'): True, } assert result == expected @@ -503,6 +513,7 @@ def testWhichRaiseStatements() -> None: 'm.n.ValueError', ], (135, 0, 'func15'): ['other.Exception'], + (141, 0, 'func16'): ['IOError', 'IndexError', 'KeyError'], } assert result == expected