Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion crates/ruff_linter/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ mod tests {
use crate::registry::Rule;
use crate::rules::{isort, pyupgrade};
use crate::settings::types::PreviewMode;
use crate::test::{test_path, test_snippet};
use crate::source_kind::SourceKind;
use crate::test::{test_contents, test_path, test_snippet};
use crate::{assert_diagnostics, assert_diagnostics_diff, settings};

#[test_case(Rule::ConvertNamedTupleFunctionalToClass, Path::new("UP014.py"))]
Expand Down Expand Up @@ -128,6 +129,44 @@ mod tests {
Ok(())
}

#[test_case(PythonVersion::PY314)]
#[test_case(PythonVersion::PY315)]
fn deprecated_import_type_form(target_version: PythonVersion) {
for (source, expected) in [
(
"from typing_extensions import TypeForm\n",
"from typing import TypeForm\n",
),
(
"from typing_extensions import TypeForm as TF\n",
"from typing import TypeForm as TF\n",
),
(
"from typing_extensions import TypeForm, Sentinel\n",
"from typing_extensions import Sentinel\nfrom typing import TypeForm\n",
),
] {
let source_kind = SourceKind::Python {
code: source.to_string(),
is_stub: false,
};
let (diagnostics, fixed) = test_contents(
&source_kind,
Path::new("example.py"),
&settings::LinterSettings::for_rule(Rule::DeprecatedImport)
.with_target_version(target_version),
);

if target_version >= PythonVersion::PY315 {
assert_eq!(diagnostics.len(), 1);
assert_eq!(fixed.source_code(), expected);
} else {
assert!(diagnostics.is_empty());
assert_eq!(fixed.source_code(), source);
}
}
}

#[test_case(PythonVersion::PY37)]
#[test_case(PythonVersion::PY312)]
#[test_case(PythonVersion::PY313)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ const TYPING_EXTENSIONS_TO_TYPES_313: &[&str] = &["CapsuleType"];
// Members of typing_extensions that were moved to `warnings`
const TYPING_EXTENSIONS_TO_WARNINGS_313: &[&str] = &["deprecated"];

// Python 3.15+

// Members of `typing_extensions` that were moved to `typing`.
const TYPING_EXTENSIONS_TO_TYPING_315: &[&str] = &["TypeForm"];

struct ImportReplacer<'a> {
import_from_stmt: &'a StmtImportFrom,
module: &'a str,
Expand Down Expand Up @@ -551,6 +556,9 @@ impl<'a> ImportReplacer<'a> {
if self.version >= PythonVersion::PY313 {
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_313);
}
if self.version >= PythonVersion::PY315 {
typing_extensions_to_typing.extend(TYPING_EXTENSIONS_TO_TYPING_315);
}
if let Some(operation) = self.try_replace(&typing_extensions_to_typing, "typing") {
operations.push(operation);
}
Expand Down