From d5a51b4e45f2fd6afb840dcc25827d9b0e5e60db Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 21 Aug 2023 10:57:34 -0400 Subject: [PATCH] Allow `ctypes.WinError()` in flake8-raise (#6731) Closes https://github.com/astral-sh/ruff/issues/6730. --- .../resources/test/fixtures/flake8_raise/RSE102.py | 7 +++++++ .../rules/unnecessary_paren_on_raise_exception.rs | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/ruff/resources/test/fixtures/flake8_raise/RSE102.py b/crates/ruff/resources/test/fixtures/flake8_raise/RSE102.py index ce75d7ab7c..38cead687f 100644 --- a/crates/ruff/resources/test/fixtures/flake8_raise/RSE102.py +++ b/crates/ruff/resources/test/fixtures/flake8_raise/RSE102.py @@ -52,3 +52,10 @@ class Class: # OK raise Class.error() + + +import ctypes + + +# OK +raise ctypes.WinError(1) diff --git a/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs b/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs index 8c0d69bf45..b1374367c4 100644 --- a/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs +++ b/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs @@ -71,6 +71,16 @@ pub(crate) fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr: return; } + // `ctypes.WinError()` is a function, not a class. It's part of the standard library, so + // we might as well get it right. + if checker + .semantic() + .resolve_call_path(func) + .is_some_and(|call_path| matches!(call_path.as_slice(), ["ctypes", "WinError"])) + { + return; + } + let range = match_parens(func.end(), checker.locator(), checker.source_type) .expect("Expected call to include parentheses"); let mut diagnostic = Diagnostic::new(UnnecessaryParenOnRaiseException, range);