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

Avoid removing un-aliased exceptions in OSError-aliased handlers #3451

Merged
merged 1 commit into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP024_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import socket

from kombu import Connection, exceptions

try:
conn = Connection(settings.CELERY_BROKER_URL)
conn.ensure_connection(max_retries=2)
conn._close()
except (socket.error, exceptions.OperationalError):
return HttpResponseServerError("cache: cannot connect to broker.")
6 changes: 3 additions & 3 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ where
}
if self.settings.rules.enabled(&Rule::OSErrorAlias) {
if let Some(item) = exc {
pyupgrade::rules::os_error_alias(self, &item);
pyupgrade::rules::os_error_alias_raise(self, item);
}
}
if self.settings.rules.enabled(&Rule::RaiseVanillaClass) {
Expand Down Expand Up @@ -1757,7 +1757,7 @@ where
flake8_bugbear::rules::redundant_tuple_in_exception_handler(self, handlers);
}
if self.settings.rules.enabled(&Rule::OSErrorAlias) {
pyupgrade::rules::os_error_alias(self, &handlers);
pyupgrade::rules::os_error_alias_handlers(self, handlers);
}
if self.settings.rules.enabled(&Rule::AssertInExcept) {
self.diagnostics.extend(
Expand Down Expand Up @@ -2484,7 +2484,7 @@ where
pyupgrade::rules::replace_stdout_stderr(self, expr, func, keywords);
}
if self.settings.rules.enabled(&Rule::OSErrorAlias) {
pyupgrade::rules::os_error_alias(self, &expr);
pyupgrade::rules::os_error_alias_call(self, func);
}
if self.settings.rules.enabled(&Rule::IsinstanceWithTuple)
&& self.settings.target_version >= PythonVersion::Py310
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod tests {
#[test_case(Rule::OSErrorAlias, Path::new("UP024_1.py"); "UP024_1")]
#[test_case(Rule::OSErrorAlias, Path::new("UP024_2.py"); "UP024_2")]
#[test_case(Rule::OSErrorAlias, Path::new("UP024_3.py"); "UP024_3")]
#[test_case(Rule::OSErrorAlias, Path::new("UP024_4.py"); "UP024_4")]
#[test_case(Rule::RewriteUnicodeLiteral, Path::new("UP025.py"); "UP025")]
#[test_case(Rule::RewriteMockImport, Path::new("UP026.py"); "UP026")]
#[test_case(Rule::RewriteListComprehension, Path::new("UP027.py"); "UP027")]
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff/src/rules/pyupgrade/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub(crate) use lru_cache_without_parameters::{
};
pub(crate) use native_literals::{native_literals, NativeLiterals};
pub(crate) use open_alias::{open_alias, OpenAlias};
pub(crate) use os_error_alias::{os_error_alias, OSErrorAlias};
pub(crate) use os_error_alias::{
os_error_alias_call, os_error_alias_handlers, os_error_alias_raise, OSErrorAlias,
};
pub(crate) use outdated_version_block::{outdated_version_block, OutdatedVersionBlock};
pub(crate) use printf_string_formatting::{printf_string_formatting, PrintfStringFormatting};
pub(crate) use quoted_annotation::{quoted_annotation, QuotedAnnotation};
Expand Down
Loading