mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
Handle non-from __future__ imports (#2974)
These are uncommon, but currently panic. Closes #2967.
This commit is contained in:
parent
e6722f92ed
commit
66a162fa40
3 changed files with 49 additions and 6 deletions
|
@ -4,3 +4,5 @@ from __future__ import absolute_import
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import __future__
|
||||||
|
|
|
@ -900,7 +900,38 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
for alias in names {
|
for alias in names {
|
||||||
if alias.node.name.contains('.') && alias.node.asname.is_none() {
|
if alias.node.name == "__future__" {
|
||||||
|
let name = alias.node.asname.as_ref().unwrap_or(&alias.node.name);
|
||||||
|
self.add_binding(
|
||||||
|
name,
|
||||||
|
Binding {
|
||||||
|
kind: BindingKind::FutureImportation,
|
||||||
|
runtime_usage: None,
|
||||||
|
// Always mark `__future__` imports as used.
|
||||||
|
synthetic_usage: Some((
|
||||||
|
self.scopes[*(self
|
||||||
|
.scope_stack
|
||||||
|
.last()
|
||||||
|
.expect("No current scope found"))]
|
||||||
|
.id,
|
||||||
|
Range::from_located(alias),
|
||||||
|
)),
|
||||||
|
typing_usage: None,
|
||||||
|
range: Range::from_located(alias),
|
||||||
|
source: Some(self.current_stmt().clone()),
|
||||||
|
context: self.execution_context(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if self.settings.rules.enabled(&Rule::LateFutureImport)
|
||||||
|
&& !self.futures_allowed
|
||||||
|
{
|
||||||
|
self.diagnostics.push(Diagnostic::new(
|
||||||
|
pyflakes::rules::LateFutureImport,
|
||||||
|
Range::from_located(stmt),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else if alias.node.name.contains('.') && alias.node.asname.is_none() {
|
||||||
// Given `import foo.bar`, `name` would be "foo", and `full_name` would be
|
// Given `import foo.bar`, `name` would be "foo", and `full_name` would be
|
||||||
// "foo.bar".
|
// "foo.bar".
|
||||||
let name = alias.node.name.split('.').next().unwrap();
|
let name = alias.node.name.split('.').next().unwrap();
|
||||||
|
@ -918,10 +949,6 @@ where
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
if let Some(asname) = &alias.node.asname {
|
|
||||||
self.check_builtin_shadowing(asname, stmt, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Given `import foo`, `name` and `full_name` would both be `foo`.
|
// Given `import foo`, `name` and `full_name` would both be `foo`.
|
||||||
// Given `import foo as bar`, `name` would be `bar` and `full_name` would
|
// Given `import foo as bar`, `name` would be `bar` and `full_name` would
|
||||||
// be `foo`.
|
// be `foo`.
|
||||||
|
@ -957,6 +984,10 @@ where
|
||||||
context: self.execution_context(),
|
context: self.execution_context(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(asname) = &alias.node.asname {
|
||||||
|
self.check_builtin_shadowing(asname, stmt, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// flake8-debugger
|
// flake8-debugger
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
source: src/rules/pyflakes/mod.rs
|
source: crates/ruff/src/rules/pyflakes/mod.rs
|
||||||
expression: diagnostics
|
expression: diagnostics
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
|
@ -12,4 +12,14 @@ expression: diagnostics
|
||||||
column: 37
|
column: 37
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
LateFutureImport: ~
|
||||||
|
location:
|
||||||
|
row: 8
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 8
|
||||||
|
column: 17
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue