Skip to content

Commit 0b235d0

Browse files
committed
Revert "bail out less aggressively for UP035"
This reverts commit 9cdc98f.
1 parent 9cdc98f commit 0b235d0

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_import.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ struct ImportReplacer<'a> {
406406
locator: &'a Locator<'a>,
407407
stylist: &'a Stylist<'a>,
408408
tokens: &'a Tokens,
409-
version: Option<PythonVersion>,
409+
version: PythonVersion,
410410
}
411411

412412
impl<'a> ImportReplacer<'a> {
@@ -416,7 +416,7 @@ impl<'a> ImportReplacer<'a> {
416416
locator: &'a Locator<'a>,
417417
stylist: &'a Stylist<'a>,
418418
tokens: &'a Tokens,
419-
version: Option<PythonVersion>,
419+
version: PythonVersion,
420420
) -> Self {
421421
Self {
422422
import_from_stmt,
@@ -432,7 +432,7 @@ impl<'a> ImportReplacer<'a> {
432432
fn with_renames(&self) -> Vec<WithRename> {
433433
let mut operations = vec![];
434434
if self.module == "typing" {
435-
if self.version.is_some_and(|v| v >= PythonVersion::PY39) {
435+
if self.version >= PythonVersion::PY39 {
436436
for member in &self.import_from_stmt.names {
437437
if let Some(target) = TYPING_TO_RENAME_PY39.iter().find_map(|(name, target)| {
438438
if &member.name == *name {
@@ -470,7 +470,7 @@ impl<'a> ImportReplacer<'a> {
470470
"typing_extensions" => {
471471
// `typing_extensions` to `collections.abc`
472472
let mut typing_extensions_to_collections_abc = vec![];
473-
if self.version.is_some_and(|v| v >= PythonVersion::PY312) {
473+
if self.version >= PythonVersion::PY312 {
474474
typing_extensions_to_collections_abc
475475
.extend(TYPING_EXTENSIONS_TO_COLLECTIONS_ABC_312);
476476
}
@@ -482,7 +482,7 @@ impl<'a> ImportReplacer<'a> {
482482

483483
// `typing_extensions` to `warnings`
484484
let mut typing_extensions_to_warnings = vec![];
485-
if self.version.is_some_and(|v| v >= PythonVersion::PY313) {
485+
if self.version >= PythonVersion::PY313 {
486486
typing_extensions_to_warnings.extend(TYPING_EXTENSIONS_TO_WARNINGS_313);
487487
}
488488
if let Some(operation) =
@@ -493,10 +493,10 @@ impl<'a> ImportReplacer<'a> {
493493

494494
// `typing_extensions` to `types`
495495
let mut typing_extensions_to_types = vec![];
496-
if self.version.is_some_and(|v| v >= PythonVersion::PY312) {
496+
if self.version >= PythonVersion::PY312 {
497497
typing_extensions_to_types.extend(TYPING_EXTENSIONS_TO_TYPES_312);
498498
}
499-
if self.version.is_some_and(|v| v >= PythonVersion::PY313) {
499+
if self.version >= PythonVersion::PY313 {
500500
typing_extensions_to_types.extend(TYPING_EXTENSIONS_TO_TYPES_313);
501501
}
502502
if let Some(operation) = self.try_replace(&typing_extensions_to_types, "types") {
@@ -505,25 +505,25 @@ impl<'a> ImportReplacer<'a> {
505505

506506
// `typing_extensions` to `typing`
507507
let mut typing_extensions_to_typing = TYPING_EXTENSIONS_TO_TYPING.to_vec();
508-
if self.version.is_some_and(|v| v >= PythonVersion::PY37) {
508+
if self.version >= PythonVersion::PY37 {
509509
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_37);
510510
}
511-
if self.version.is_some_and(|v| v >= PythonVersion::PY38) {
511+
if self.version >= PythonVersion::PY38 {
512512
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_38);
513513
}
514-
if self.version.is_some_and(|v| v >= PythonVersion::PY39) {
514+
if self.version >= PythonVersion::PY39 {
515515
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_39);
516516
}
517-
if self.version.is_some_and(|v| v >= PythonVersion::PY310) {
517+
if self.version >= PythonVersion::PY310 {
518518
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_310);
519519
}
520-
if self.version.is_some_and(|v| v >= PythonVersion::PY311) {
520+
if self.version >= PythonVersion::PY311 {
521521
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_311);
522522
}
523-
if self.version.is_some_and(|v| v >= PythonVersion::PY312) {
523+
if self.version >= PythonVersion::PY312 {
524524
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_312);
525525
}
526-
if self.version.is_some_and(|v| v >= PythonVersion::PY313) {
526+
if self.version >= PythonVersion::PY313 {
527527
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_313);
528528
}
529529
if let Some(operation) = self.try_replace(&typing_extensions_to_typing, "typing") {
@@ -532,10 +532,10 @@ impl<'a> ImportReplacer<'a> {
532532
}
533533
"mypy_extensions" => {
534534
let mut mypy_extensions_to_typing = vec![];
535-
if self.version.is_some_and(|v| v >= PythonVersion::PY37) {
535+
if self.version >= PythonVersion::PY37 {
536536
mypy_extensions_to_typing.extend(MYPY_EXTENSIONS_TO_TYPING_37);
537537
}
538-
if self.version.is_some_and(|v| v >= PythonVersion::PY38) {
538+
if self.version >= PythonVersion::PY38 {
539539
mypy_extensions_to_typing.extend(MYPY_EXTENSIONS_TO_TYPING_38);
540540
}
541541
if let Some(operation) = self.try_replace(&mypy_extensions_to_typing, "typing") {
@@ -545,10 +545,10 @@ impl<'a> ImportReplacer<'a> {
545545
"typing" => {
546546
// `typing` to `collections.abc`
547547
let mut typing_to_collections_abc = vec![];
548-
if self.version.is_some_and(|v| v >= PythonVersion::PY39) {
548+
if self.version >= PythonVersion::PY39 {
549549
typing_to_collections_abc.extend(TYPING_TO_COLLECTIONS_ABC_39);
550550
}
551-
if self.version.is_some_and(|v| v >= PythonVersion::PY310) {
551+
if self.version >= PythonVersion::PY310 {
552552
typing_to_collections_abc.extend(TYPING_TO_COLLECTIONS_ABC_310);
553553
}
554554
if let Some(operation) =
@@ -559,7 +559,7 @@ impl<'a> ImportReplacer<'a> {
559559

560560
// `typing` to `collections`
561561
let mut typing_to_collections = vec![];
562-
if self.version.is_some_and(|v| v >= PythonVersion::PY39) {
562+
if self.version >= PythonVersion::PY39 {
563563
typing_to_collections.extend(TYPING_TO_COLLECTIONS_39);
564564
}
565565
if let Some(operation) = self.try_replace(&typing_to_collections, "collections") {
@@ -568,19 +568,19 @@ impl<'a> ImportReplacer<'a> {
568568

569569
// `typing` to `re`
570570
let mut typing_to_re = vec![];
571-
if self.version.is_some_and(|v| v >= PythonVersion::PY39) {
571+
if self.version >= PythonVersion::PY39 {
572572
typing_to_re.extend(TYPING_TO_RE_39);
573573
}
574574
if let Some(operation) = self.try_replace(&typing_to_re, "re") {
575575
operations.push(operation);
576576
}
577577
}
578-
"typing.re" if self.version.is_some_and(|v| v >= PythonVersion::PY39) => {
578+
"typing.re" if self.version >= PythonVersion::PY39 => {
579579
if let Some(operation) = self.try_replace(TYPING_RE_TO_RE_39, "re") {
580580
operations.push(operation);
581581
}
582582
}
583-
"backports.strenum" if self.version.is_some_and(|v| v >= PythonVersion::PY311) => {
583+
"backports.strenum" if self.version >= PythonVersion::PY311 => {
584584
if let Some(operation) = self.try_replace(BACKPORTS_STR_ENUM_TO_ENUM_311, "enum") {
585585
operations.push(operation);
586586
}
@@ -716,13 +716,17 @@ pub(crate) fn deprecated_import(checker: &Checker, import_from_stmt: &StmtImport
716716
return;
717717
}
718718

719+
let Some(target_version) = checker.target_version() else {
720+
return;
721+
};
722+
719723
let fixer = ImportReplacer::new(
720724
import_from_stmt,
721725
module,
722726
checker.locator(),
723727
checker.stylist(),
724728
checker.tokens(),
725-
checker.target_version(),
729+
target_version,
726730
);
727731

728732
for (operation, fix) in fixer.without_renames() {

0 commit comments

Comments
 (0)