Add config option to disable typing_extensions imports (#17611)

Summary
--

This PR resolves https://github.com/astral-sh/ruff/issues/9761 by adding
a linter configuration option to disable
`typing_extensions` imports. As mentioned [here], it would be ideal if
we could
detect whether or not `typing_extensions` is available as a dependency
automatically, but this seems like a much easier fix in the meantime.

The default for the new option, `typing-extensions`, is `true`,
preserving the current behavior. Setting it to `false` will bail out of
the new
`Checker::typing_importer` method, which has been refactored from the 
`Checker::import_from_typing` method in
https://github.com/astral-sh/ruff/pull/17340),
with `None`, which is then handled specially by each rule that calls it.

I considered some alternatives to a config option, such as checking if
`typing_extensions` has been imported or checking for a `TYPE_CHECKING`
block we could use, but I think defaulting to allowing
`typing_extensions` imports and allowing the user to disable this with
an option is both simple to implement and pretty intuitive.

[here]:
https://github.com/astral-sh/ruff/issues/9761#issuecomment-2790492853

Test Plan
--

New linter tests exercising several combinations of Python versions and
the new config option for PYI019. I also added tests for the other
affected rules, but only in the case where the new config option is
enabled. The rules' existing tests also cover the default case.
This commit is contained in:
Brent Westbrook 2025-04-28 14:57:36 -04:00 committed by GitHub
parent 405878a128
commit 01a31c08f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 470 additions and 160 deletions

View file

@ -430,6 +430,7 @@ impl Configuration {
.ruff
.map(RuffOptions::into_settings)
.unwrap_or_default(),
typing_extensions: lint.typing_extensions.unwrap_or(true),
},
formatter,
@ -633,6 +634,7 @@ pub struct LintConfiguration {
pub logger_objects: Option<Vec<String>>,
pub task_tags: Option<Vec<String>>,
pub typing_modules: Option<Vec<String>>,
pub typing_extensions: Option<bool>,
// Plugins
pub flake8_annotations: Option<Flake8AnnotationsOptions>,
@ -746,6 +748,7 @@ impl LintConfiguration {
task_tags: options.common.task_tags,
logger_objects: options.common.logger_objects,
typing_modules: options.common.typing_modules,
typing_extensions: options.typing_extensions,
// Plugins
flake8_annotations: options.common.flake8_annotations,
@ -1170,6 +1173,7 @@ impl LintConfiguration {
pylint: self.pylint.combine(config.pylint),
pyupgrade: self.pyupgrade.combine(config.pyupgrade),
ruff: self.ruff.combine(config.ruff),
typing_extensions: self.typing_extensions,
}
}
}

View file

@ -513,6 +513,22 @@ pub struct LintOptions {
"#
)]
pub preview: Option<bool>,
/// Whether to allow imports from the third-party `typing_extensions` module for Python versions
/// before a symbol was added to the first-party `typing` module.
///
/// Many rules try to import symbols from the `typing` module but fall back to
/// `typing_extensions` for earlier versions of Python. This option can be used to disable this
/// fallback behavior in cases where `typing_extensions` is not installed.
#[option(
default = "true",
value_type = "bool",
example = r#"
# Disable `typing_extensions` imports
typing-extensions = false
"#
)]
pub typing_extensions: Option<bool>,
}
/// Newtype wrapper for [`LintCommonOptions`] that allows customizing the JSON schema and omitting the fields from the [`OptionsMetadata`].
@ -3876,6 +3892,7 @@ pub struct LintOptionsWire {
pydoclint: Option<PydoclintOptions>,
ruff: Option<RuffOptions>,
preview: Option<bool>,
typing_extensions: Option<bool>,
}
impl From<LintOptionsWire> for LintOptions {
@ -3930,6 +3947,7 @@ impl From<LintOptionsWire> for LintOptions {
pydoclint,
ruff,
preview,
typing_extensions,
} = value;
LintOptions {
@ -3985,6 +4003,7 @@ impl From<LintOptionsWire> for LintOptions {
pydoclint,
ruff,
preview,
typing_extensions,
}
}
}