Allow removal of typing from exempt-modules (#9214)

## Summary

If you remove `typing` from `exempt-modules`, we tend to panic, since we
try to add `TYPE_CHECKING` to `from typing import ...` statements while
concurrently attempting to remove other members from that import. This
PR adds special-casing for typing imports to avoid such panics.

Closes https://github.com/astral-sh/ruff/issues/5331
Closes https://github.com/astral-sh/ruff/issues/9196.
Closes https://github.com/astral-sh/ruff/issues/9197.
This commit is contained in:
Charlie Marsh 2023-12-20 11:03:02 -05:00 committed by GitHub
parent 29846f5b09
commit 4b4160eb48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 235 additions and 35 deletions

View file

@ -761,6 +761,7 @@ impl<'a> SemanticModel<'a> {
{
return Some(ImportedName {
name: format!("{name}.{member}"),
source,
range: self.nodes[source].range(),
context: binding.context,
});
@ -785,6 +786,7 @@ impl<'a> SemanticModel<'a> {
{
return Some(ImportedName {
name: (*name).to_string(),
source,
range: self.nodes[source].range(),
context: binding.context,
});
@ -806,6 +808,7 @@ impl<'a> SemanticModel<'a> {
{
return Some(ImportedName {
name: format!("{name}.{member}"),
source,
range: self.nodes[source].range(),
context: binding.context,
});
@ -1828,6 +1831,8 @@ pub enum ReadResult {
pub struct ImportedName {
/// The name to which the imported symbol is bound.
name: String,
/// The statement from which the symbol is imported.
source: NodeId,
/// The range at which the symbol is imported.
range: TextRange,
/// The context in which the symbol is imported.
@ -1842,6 +1847,10 @@ impl ImportedName {
pub const fn context(&self) -> ExecutionContext {
self.context
}
pub fn statement<'a>(&self, semantic: &'a SemanticModel) -> &'a Stmt {
semantic.statement(self.source)
}
}
impl Ranged for ImportedName {