mirror of https://github.com/astral-sh/ruff
Rename `*Importation` structs to `*Import` (#5185)
## Summary I find "Importation" a bit awkward, it may not even be grammatically correct here.
This commit is contained in:
parent
e3c12764f8
commit
94abf7f088
|
|
@ -22,9 +22,8 @@ use ruff_python_ast::{cast, helpers, identifier, str, visitor};
|
||||||
use ruff_python_semantic::analyze::{branch_detection, typing, visibility};
|
use ruff_python_semantic::analyze::{branch_detection, typing, visibility};
|
||||||
use ruff_python_semantic::{
|
use ruff_python_semantic::{
|
||||||
Binding, BindingFlags, BindingId, BindingKind, ContextualizedDefinition, Exceptions,
|
Binding, BindingFlags, BindingId, BindingKind, ContextualizedDefinition, Exceptions,
|
||||||
ExecutionContext, Export, FromImportation, Globals, Importation, Module, ModuleKind,
|
ExecutionContext, Export, FromImport, Globals, Import, Module, ModuleKind, ResolvedRead, Scope,
|
||||||
ResolvedRead, Scope, ScopeId, ScopeKind, SemanticModel, SemanticModelFlags, StarImportation,
|
ScopeId, ScopeKind, SemanticModel, SemanticModelFlags, StarImport, SubmoduleImport,
|
||||||
SubmoduleImportation,
|
|
||||||
};
|
};
|
||||||
use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
|
use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
|
||||||
use ruff_python_stdlib::path::is_python_stub_file;
|
use ruff_python_stdlib::path::is_python_stub_file;
|
||||||
|
|
@ -815,9 +814,7 @@ where
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
name,
|
name,
|
||||||
alias.identifier(self.locator),
|
alias.identifier(self.locator),
|
||||||
BindingKind::SubmoduleImportation(SubmoduleImportation {
|
BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }),
|
||||||
qualified_name,
|
|
||||||
}),
|
|
||||||
BindingFlags::EXTERNAL,
|
BindingFlags::EXTERNAL,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -838,7 +835,7 @@ where
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
name,
|
name,
|
||||||
alias.identifier(self.locator),
|
alias.identifier(self.locator),
|
||||||
BindingKind::Importation(Importation { qualified_name }),
|
BindingKind::Import(Import { qualified_name }),
|
||||||
flags,
|
flags,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -1056,7 +1053,7 @@ where
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
name,
|
name,
|
||||||
alias.identifier(self.locator),
|
alias.identifier(self.locator),
|
||||||
BindingKind::FutureImportation,
|
BindingKind::FutureImport,
|
||||||
BindingFlags::empty(),
|
BindingFlags::empty(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -1074,7 +1071,7 @@ where
|
||||||
} else if &alias.name == "*" {
|
} else if &alias.name == "*" {
|
||||||
self.semantic
|
self.semantic
|
||||||
.scope_mut()
|
.scope_mut()
|
||||||
.add_star_import(StarImportation { level, module });
|
.add_star_import(StarImport { level, module });
|
||||||
|
|
||||||
if self.enabled(Rule::UndefinedLocalWithNestedImportStarUsage) {
|
if self.enabled(Rule::UndefinedLocalWithNestedImportStarUsage) {
|
||||||
let scope = self.semantic.scope();
|
let scope = self.semantic.scope();
|
||||||
|
|
@ -1127,7 +1124,7 @@ where
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
name,
|
name,
|
||||||
alias.identifier(self.locator),
|
alias.identifier(self.locator),
|
||||||
BindingKind::FromImportation(FromImportation { qualified_name }),
|
BindingKind::FromImport(FromImport { qualified_name }),
|
||||||
flags,
|
flags,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -4190,10 +4187,10 @@ impl<'a> Checker<'a> {
|
||||||
{
|
{
|
||||||
let shadows_import = matches!(
|
let shadows_import = matches!(
|
||||||
shadowed.kind,
|
shadowed.kind,
|
||||||
BindingKind::Importation(..)
|
BindingKind::Import(..)
|
||||||
| BindingKind::FromImportation(..)
|
| BindingKind::FromImport(..)
|
||||||
| BindingKind::SubmoduleImportation(..)
|
| BindingKind::SubmoduleImport(..)
|
||||||
| BindingKind::FutureImportation
|
| BindingKind::FutureImport
|
||||||
);
|
);
|
||||||
if binding.kind.is_loop_var() && shadows_import {
|
if binding.kind.is_loop_var() && shadows_import {
|
||||||
if self.enabled(Rule::ImportShadowedByLoopVar) {
|
if self.enabled(Rule::ImportShadowedByLoopVar) {
|
||||||
|
|
@ -4314,7 +4311,7 @@ impl<'a> Checker<'a> {
|
||||||
.scopes
|
.scopes
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(Scope::star_imports)
|
.flat_map(Scope::star_imports)
|
||||||
.map(|StarImportation { level, module }| {
|
.map(|StarImport { level, module }| {
|
||||||
helpers::format_import_from(*level, *module)
|
helpers::format_import_from(*level, *module)
|
||||||
})
|
})
|
||||||
.sorted()
|
.sorted()
|
||||||
|
|
@ -4792,7 +4789,7 @@ impl<'a> Checker<'a> {
|
||||||
if self.enabled(Rule::UndefinedLocalWithImportStarUsage) {
|
if self.enabled(Rule::UndefinedLocalWithImportStarUsage) {
|
||||||
let sources: Vec<String> = scope
|
let sources: Vec<String> = scope
|
||||||
.star_imports()
|
.star_imports()
|
||||||
.map(|StarImportation { level, module }| {
|
.map(|StarImport { level, module }| {
|
||||||
helpers::format_import_from(*level, *module)
|
helpers::format_import_from(*level, *module)
|
||||||
})
|
})
|
||||||
.sorted()
|
.sorted()
|
||||||
|
|
|
||||||
|
|
@ -217,7 +217,7 @@ impl Renamer {
|
||||||
/// Rename a [`Binding`] reference.
|
/// Rename a [`Binding`] reference.
|
||||||
fn rename_binding(binding: &Binding, name: &str, target: &str) -> Option<Edit> {
|
fn rename_binding(binding: &Binding, name: &str, target: &str) -> Option<Edit> {
|
||||||
match &binding.kind {
|
match &binding.kind {
|
||||||
BindingKind::Importation(_) | BindingKind::FromImportation(_) => {
|
BindingKind::Import(_) | BindingKind::FromImport(_) => {
|
||||||
if binding.is_alias() {
|
if binding.is_alias() {
|
||||||
// Ex) Rename `import pandas as alias` to `import pandas as pd`.
|
// Ex) Rename `import pandas as alias` to `import pandas as pd`.
|
||||||
Some(Edit::range_replacement(target.to_string(), binding.range))
|
Some(Edit::range_replacement(target.to_string(), binding.range))
|
||||||
|
|
@ -229,7 +229,7 @@ impl Renamer {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BindingKind::SubmoduleImportation(import) => {
|
BindingKind::SubmoduleImport(import) => {
|
||||||
// Ex) Rename `import pandas.core` to `import pandas as pd`.
|
// Ex) Rename `import pandas.core` to `import pandas as pd`.
|
||||||
let module_name = import.qualified_name.split('.').next().unwrap();
|
let module_name = import.qualified_name.split('.').next().unwrap();
|
||||||
Some(Edit::range_replacement(
|
Some(Edit::range_replacement(
|
||||||
|
|
@ -238,7 +238,7 @@ impl Renamer {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
// Avoid renaming builtins and other "special" bindings.
|
// Avoid renaming builtins and other "special" bindings.
|
||||||
BindingKind::FutureImportation | BindingKind::Builtin | BindingKind::Export(_) => None,
|
BindingKind::FutureImport | BindingKind::Builtin | BindingKind::Export(_) => None,
|
||||||
// By default, replace the binding's name with the target name.
|
// By default, replace the binding's name with the target name.
|
||||||
BindingKind::Annotation
|
BindingKind::Annotation
|
||||||
| BindingKind::Argument
|
| BindingKind::Argument
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Fix, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_semantic::{BindingKind, FromImportation, Scope};
|
use ruff_python_semantic::{BindingKind, FromImport, Scope};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
@ -53,7 +53,7 @@ pub(crate) fn unaliased_collections_abc_set_import(
|
||||||
) {
|
) {
|
||||||
for (name, binding_id) in scope.all_bindings() {
|
for (name, binding_id) in scope.all_bindings() {
|
||||||
let binding = checker.semantic().binding(binding_id);
|
let binding = checker.semantic().binding(binding_id);
|
||||||
let BindingKind::FromImportation(FromImportation { qualified_name }) = &binding.kind else {
|
let BindingKind::FromImport(FromImport { qualified_name }) = &binding.kind else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if qualified_name.as_str() != "collections.abc.Set" {
|
if qualified_name.as_str() != "collections.abc.Set" {
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,7 @@ use ruff_python_semantic::{Binding, BindingKind, ScopeKind, SemanticModel};
|
||||||
pub(crate) fn is_valid_runtime_import(binding: &Binding, semantic: &SemanticModel) -> bool {
|
pub(crate) fn is_valid_runtime_import(binding: &Binding, semantic: &SemanticModel) -> bool {
|
||||||
if matches!(
|
if matches!(
|
||||||
binding.kind,
|
binding.kind,
|
||||||
BindingKind::Importation(..)
|
BindingKind::Import(..) | BindingKind::FromImport(..) | BindingKind::SubmoduleImport(..)
|
||||||
| BindingKind::FromImportation(..)
|
|
||||||
| BindingKind::SubmoduleImportation(..)
|
|
||||||
) {
|
) {
|
||||||
binding.context.is_runtime()
|
binding.context.is_runtime()
|
||||||
&& binding
|
&& binding
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use rustpython_parser::ast;
|
use rustpython_parser::ast;
|
||||||
use rustpython_parser::ast::Expr;
|
use rustpython_parser::ast::Expr;
|
||||||
|
|
||||||
use ruff_python_semantic::{BindingKind, Importation, SemanticModel};
|
use ruff_python_semantic::{BindingKind, Import, SemanticModel};
|
||||||
|
|
||||||
pub(super) enum Resolution {
|
pub(super) enum Resolution {
|
||||||
/// The expression resolves to an irrelevant expression type (e.g., a constant).
|
/// The expression resolves to an irrelevant expression type (e.g., a constant).
|
||||||
|
|
@ -39,7 +39,7 @@ pub(super) fn test_expression(expr: &Expr, semantic: &SemanticModel) -> Resoluti
|
||||||
| BindingKind::LoopVar
|
| BindingKind::LoopVar
|
||||||
| BindingKind::Global
|
| BindingKind::Global
|
||||||
| BindingKind::Nonlocal(_) => Resolution::RelevantLocal,
|
| BindingKind::Nonlocal(_) => Resolution::RelevantLocal,
|
||||||
BindingKind::Importation(Importation {
|
BindingKind::Import(Import {
|
||||||
qualified_name: module,
|
qualified_name: module,
|
||||||
}) if module == "pandas" => Resolution::PandasModule,
|
}) if module == "pandas" => Resolution::PandasModule,
|
||||||
_ => Resolution::IrrelevantBinding,
|
_ => Resolution::IrrelevantBinding,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged};
|
||||||
|
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_semantic::{BindingKind, Importation};
|
use ruff_python_semantic::{BindingKind, Import};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
@ -70,7 +70,7 @@ pub(crate) fn inplace_argument(
|
||||||
.map_or(false, |binding| {
|
.map_or(false, |binding| {
|
||||||
matches!(
|
matches!(
|
||||||
binding.kind,
|
binding.kind,
|
||||||
BindingKind::Importation(Importation {
|
BindingKind::Import(Import {
|
||||||
qualified_name: "pandas"
|
qualified_name: "pandas"
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -82,33 +82,33 @@ impl<'a> Binding<'a> {
|
||||||
/// Return `true` if this binding redefines the given binding.
|
/// Return `true` if this binding redefines the given binding.
|
||||||
pub fn redefines(&self, existing: &'a Binding) -> bool {
|
pub fn redefines(&self, existing: &'a Binding) -> bool {
|
||||||
match &self.kind {
|
match &self.kind {
|
||||||
BindingKind::Importation(Importation { qualified_name }) => {
|
BindingKind::Import(Import { qualified_name }) => {
|
||||||
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
|
if let BindingKind::SubmoduleImport(SubmoduleImport {
|
||||||
qualified_name: existing,
|
qualified_name: existing,
|
||||||
}) = &existing.kind
|
}) = &existing.kind
|
||||||
{
|
{
|
||||||
return qualified_name == existing;
|
return qualified_name == existing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BindingKind::FromImportation(FromImportation { qualified_name }) => {
|
BindingKind::FromImport(FromImport { qualified_name }) => {
|
||||||
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
|
if let BindingKind::SubmoduleImport(SubmoduleImport {
|
||||||
qualified_name: existing,
|
qualified_name: existing,
|
||||||
}) = &existing.kind
|
}) = &existing.kind
|
||||||
{
|
{
|
||||||
return qualified_name == existing;
|
return qualified_name == existing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
|
BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }) => {
|
||||||
match &existing.kind {
|
match &existing.kind {
|
||||||
BindingKind::Importation(Importation {
|
BindingKind::Import(Import {
|
||||||
qualified_name: existing,
|
qualified_name: existing,
|
||||||
})
|
})
|
||||||
| BindingKind::SubmoduleImportation(SubmoduleImportation {
|
| BindingKind::SubmoduleImport(SubmoduleImport {
|
||||||
qualified_name: existing,
|
qualified_name: existing,
|
||||||
}) => {
|
}) => {
|
||||||
return qualified_name == existing;
|
return qualified_name == existing;
|
||||||
}
|
}
|
||||||
BindingKind::FromImportation(FromImportation {
|
BindingKind::FromImport(FromImport {
|
||||||
qualified_name: existing,
|
qualified_name: existing,
|
||||||
}) => {
|
}) => {
|
||||||
return qualified_name == existing;
|
return qualified_name == existing;
|
||||||
|
|
@ -118,7 +118,7 @@ impl<'a> Binding<'a> {
|
||||||
}
|
}
|
||||||
BindingKind::Deletion
|
BindingKind::Deletion
|
||||||
| BindingKind::Annotation
|
| BindingKind::Annotation
|
||||||
| BindingKind::FutureImportation
|
| BindingKind::FutureImport
|
||||||
| BindingKind::Builtin => {
|
| BindingKind::Builtin => {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -128,20 +128,18 @@ impl<'a> Binding<'a> {
|
||||||
existing.kind,
|
existing.kind,
|
||||||
BindingKind::ClassDefinition
|
BindingKind::ClassDefinition
|
||||||
| BindingKind::FunctionDefinition
|
| BindingKind::FunctionDefinition
|
||||||
| BindingKind::Importation(..)
|
| BindingKind::Import(..)
|
||||||
| BindingKind::FromImportation(..)
|
| BindingKind::FromImport(..)
|
||||||
| BindingKind::SubmoduleImportation(..)
|
| BindingKind::SubmoduleImport(..)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the fully-qualified symbol name, if this symbol was imported from another module.
|
/// Returns the fully-qualified symbol name, if this symbol was imported from another module.
|
||||||
pub fn qualified_name(&self) -> Option<&str> {
|
pub fn qualified_name(&self) -> Option<&str> {
|
||||||
match &self.kind {
|
match &self.kind {
|
||||||
BindingKind::Importation(Importation { qualified_name }) => Some(qualified_name),
|
BindingKind::Import(Import { qualified_name }) => Some(qualified_name),
|
||||||
BindingKind::FromImportation(FromImportation { qualified_name }) => {
|
BindingKind::FromImport(FromImport { qualified_name }) => Some(qualified_name),
|
||||||
Some(qualified_name)
|
BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }) => {
|
||||||
}
|
|
||||||
BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
|
|
||||||
Some(qualified_name)
|
Some(qualified_name)
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
@ -152,11 +150,11 @@ impl<'a> Binding<'a> {
|
||||||
/// symbol was imported from another module.
|
/// symbol was imported from another module.
|
||||||
pub fn module_name(&self) -> Option<&str> {
|
pub fn module_name(&self) -> Option<&str> {
|
||||||
match &self.kind {
|
match &self.kind {
|
||||||
BindingKind::Importation(Importation { qualified_name })
|
BindingKind::Import(Import { qualified_name })
|
||||||
| BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
|
| BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }) => {
|
||||||
Some(qualified_name.split('.').next().unwrap_or(qualified_name))
|
Some(qualified_name.split('.').next().unwrap_or(qualified_name))
|
||||||
}
|
}
|
||||||
BindingKind::FromImportation(FromImportation { qualified_name }) => Some(
|
BindingKind::FromImport(FromImport { qualified_name }) => Some(
|
||||||
qualified_name
|
qualified_name
|
||||||
.rsplit_once('.')
|
.rsplit_once('.')
|
||||||
.map_or(qualified_name, |(module, _)| module),
|
.map_or(qualified_name, |(module, _)| module),
|
||||||
|
|
@ -275,7 +273,7 @@ impl<'a> FromIterator<Binding<'a>> for Bindings<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StarImportation<'a> {
|
pub struct StarImport<'a> {
|
||||||
/// The level of the import. `None` or `Some(0)` indicate an absolute import.
|
/// The level of the import. `None` or `Some(0)` indicate an absolute import.
|
||||||
pub level: Option<u32>,
|
pub level: Option<u32>,
|
||||||
/// The module being imported. `None` indicates a wildcard import.
|
/// The module being imported. `None` indicates a wildcard import.
|
||||||
|
|
@ -292,7 +290,7 @@ pub struct Export<'a> {
|
||||||
/// Ex) `import foo` would be keyed on "foo".
|
/// Ex) `import foo` would be keyed on "foo".
|
||||||
/// Ex) `import foo as bar` would be keyed on "bar".
|
/// Ex) `import foo as bar` would be keyed on "bar".
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Importation<'a> {
|
pub struct Import<'a> {
|
||||||
/// The full name of the module being imported.
|
/// The full name of the module being imported.
|
||||||
/// Ex) Given `import foo`, `qualified_name` would be "foo".
|
/// Ex) Given `import foo`, `qualified_name` would be "foo".
|
||||||
/// Ex) Given `import foo as bar`, `qualified_name` would be "foo".
|
/// Ex) Given `import foo as bar`, `qualified_name` would be "foo".
|
||||||
|
|
@ -303,7 +301,7 @@ pub struct Importation<'a> {
|
||||||
/// Ex) `from foo import bar` would be keyed on "bar".
|
/// Ex) `from foo import bar` would be keyed on "bar".
|
||||||
/// Ex) `from foo import bar as baz` would be keyed on "baz".
|
/// Ex) `from foo import bar as baz` would be keyed on "baz".
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FromImportation {
|
pub struct FromImport {
|
||||||
/// The full name of the member being imported.
|
/// The full name of the member being imported.
|
||||||
/// Ex) Given `from foo import bar`, `qualified_name` would be "foo.bar".
|
/// Ex) Given `from foo import bar`, `qualified_name` would be "foo.bar".
|
||||||
/// Ex) Given `from foo import bar as baz`, `qualified_name` would be "foo.bar".
|
/// Ex) Given `from foo import bar as baz`, `qualified_name` would be "foo.bar".
|
||||||
|
|
@ -313,7 +311,7 @@ pub struct FromImportation {
|
||||||
/// A binding for a submodule imported from a module, keyed on the name of the parent module.
|
/// A binding for a submodule imported from a module, keyed on the name of the parent module.
|
||||||
/// Ex) `import foo.bar` would be keyed on "foo".
|
/// Ex) `import foo.bar` would be keyed on "foo".
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SubmoduleImportation<'a> {
|
pub struct SubmoduleImport<'a> {
|
||||||
/// The full name of the submodule being imported.
|
/// The full name of the submodule being imported.
|
||||||
/// Ex) Given `import foo.bar`, `qualified_name` would be "foo.bar".
|
/// Ex) Given `import foo.bar`, `qualified_name` would be "foo.bar".
|
||||||
pub qualified_name: &'a str,
|
pub qualified_name: &'a str,
|
||||||
|
|
@ -401,25 +399,25 @@ pub enum BindingKind<'a> {
|
||||||
/// ```python
|
/// ```python
|
||||||
/// from __future__ import annotations
|
/// from __future__ import annotations
|
||||||
/// ```
|
/// ```
|
||||||
FutureImportation,
|
FutureImport,
|
||||||
|
|
||||||
/// A binding for a straight `import`, like `foo` in:
|
/// A binding for a straight `import`, like `foo` in:
|
||||||
/// ```python
|
/// ```python
|
||||||
/// import foo
|
/// import foo
|
||||||
/// ```
|
/// ```
|
||||||
Importation(Importation<'a>),
|
Import(Import<'a>),
|
||||||
|
|
||||||
/// A binding for a member imported from a module, like `bar` in:
|
/// A binding for a member imported from a module, like `bar` in:
|
||||||
/// ```python
|
/// ```python
|
||||||
/// from foo import bar
|
/// from foo import bar
|
||||||
/// ```
|
/// ```
|
||||||
FromImportation(FromImportation),
|
FromImport(FromImport),
|
||||||
|
|
||||||
/// A binding for a submodule imported from a module, like `bar` in:
|
/// A binding for a submodule imported from a module, like `bar` in:
|
||||||
/// ```python
|
/// ```python
|
||||||
/// import foo.bar
|
/// import foo.bar
|
||||||
/// ```
|
/// ```
|
||||||
SubmoduleImportation(SubmoduleImportation<'a>),
|
SubmoduleImport(SubmoduleImport<'a>),
|
||||||
|
|
||||||
/// A binding for a deletion, like `x` in:
|
/// A binding for a deletion, like `x` in:
|
||||||
/// ```python
|
/// ```python
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ use ruff_python_stdlib::path::is_python_stub_file;
|
||||||
use ruff_python_stdlib::typing::is_typing_extension;
|
use ruff_python_stdlib::typing::is_typing_extension;
|
||||||
|
|
||||||
use crate::binding::{
|
use crate::binding::{
|
||||||
Binding, BindingFlags, BindingId, BindingKind, Bindings, Exceptions, FromImportation,
|
Binding, BindingFlags, BindingId, BindingKind, Bindings, Exceptions, FromImport, Import,
|
||||||
Importation, SubmoduleImportation,
|
SubmoduleImport,
|
||||||
};
|
};
|
||||||
use crate::context::ExecutionContext;
|
use crate::context::ExecutionContext;
|
||||||
use crate::definition::{Definition, DefinitionId, Definitions, Member, Module};
|
use crate::definition::{Definition, DefinitionId, Definitions, Member, Module};
|
||||||
|
|
@ -417,7 +417,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
let head = call_path.first()?;
|
let head = call_path.first()?;
|
||||||
let binding = self.find_binding(head)?;
|
let binding = self.find_binding(head)?;
|
||||||
match &binding.kind {
|
match &binding.kind {
|
||||||
BindingKind::Importation(Importation {
|
BindingKind::Import(Import {
|
||||||
qualified_name: name,
|
qualified_name: name,
|
||||||
}) => {
|
}) => {
|
||||||
if name.starts_with('.') {
|
if name.starts_with('.') {
|
||||||
|
|
@ -434,7 +434,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
Some(source_path)
|
Some(source_path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BindingKind::SubmoduleImportation(SubmoduleImportation {
|
BindingKind::SubmoduleImport(SubmoduleImport {
|
||||||
qualified_name: name,
|
qualified_name: name,
|
||||||
}) => {
|
}) => {
|
||||||
let name = name.split('.').next().unwrap_or(name);
|
let name = name.split('.').next().unwrap_or(name);
|
||||||
|
|
@ -442,7 +442,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
source_path.extend(call_path.into_iter().skip(1));
|
source_path.extend(call_path.into_iter().skip(1));
|
||||||
Some(source_path)
|
Some(source_path)
|
||||||
}
|
}
|
||||||
BindingKind::FromImportation(FromImportation {
|
BindingKind::FromImport(FromImport {
|
||||||
qualified_name: name,
|
qualified_name: name,
|
||||||
}) => {
|
}) => {
|
||||||
if name.starts_with('.') {
|
if name.starts_with('.') {
|
||||||
|
|
@ -493,7 +493,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
// Ex) Given `module="sys"` and `object="exit"`:
|
// Ex) Given `module="sys"` and `object="exit"`:
|
||||||
// `import sys` -> `sys.exit`
|
// `import sys` -> `sys.exit`
|
||||||
// `import sys as sys2` -> `sys2.exit`
|
// `import sys as sys2` -> `sys2.exit`
|
||||||
BindingKind::Importation(Importation { qualified_name }) => {
|
BindingKind::Import(Import { qualified_name }) => {
|
||||||
if qualified_name == &module {
|
if qualified_name == &module {
|
||||||
if let Some(source) = binding.source {
|
if let Some(source) = binding.source {
|
||||||
// Verify that `sys` isn't bound in an inner scope.
|
// Verify that `sys` isn't bound in an inner scope.
|
||||||
|
|
@ -514,7 +514,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
// Ex) Given `module="os.path"` and `object="join"`:
|
// Ex) Given `module="os.path"` and `object="join"`:
|
||||||
// `from os.path import join` -> `join`
|
// `from os.path import join` -> `join`
|
||||||
// `from os.path import join as join2` -> `join2`
|
// `from os.path import join as join2` -> `join2`
|
||||||
BindingKind::FromImportation(FromImportation { qualified_name }) => {
|
BindingKind::FromImport(FromImport { qualified_name }) => {
|
||||||
if let Some((target_module, target_member)) = qualified_name.split_once('.')
|
if let Some((target_module, target_member)) = qualified_name.split_once('.')
|
||||||
{
|
{
|
||||||
if target_module == module && target_member == member {
|
if target_module == module && target_member == member {
|
||||||
|
|
@ -537,7 +537,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
}
|
}
|
||||||
// Ex) Given `module="os"` and `object="name"`:
|
// Ex) Given `module="os"` and `object="name"`:
|
||||||
// `import os.path ` -> `os.name`
|
// `import os.path ` -> `os.name`
|
||||||
BindingKind::SubmoduleImportation(SubmoduleImportation { .. }) => {
|
BindingKind::SubmoduleImport(SubmoduleImport { .. }) => {
|
||||||
if name == module {
|
if name == module {
|
||||||
if let Some(source) = binding.source {
|
if let Some(source) = binding.source {
|
||||||
// Verify that `os` isn't bound in an inner scope.
|
// Verify that `os` isn't bound in an inner scope.
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use rustpython_parser::ast;
|
||||||
|
|
||||||
use ruff_index::{newtype_index, Idx, IndexSlice, IndexVec};
|
use ruff_index::{newtype_index, Idx, IndexSlice, IndexVec};
|
||||||
|
|
||||||
use crate::binding::{BindingId, StarImportation};
|
use crate::binding::{BindingId, StarImport};
|
||||||
use crate::globals::GlobalsId;
|
use crate::globals::GlobalsId;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -21,7 +21,7 @@ pub struct Scope<'a> {
|
||||||
|
|
||||||
/// A list of star imports in this scope. These represent _module_ imports (e.g., `sys` in
|
/// A list of star imports in this scope. These represent _module_ imports (e.g., `sys` in
|
||||||
/// `from sys import *`), rather than individual bindings (e.g., individual members in `sys`).
|
/// `from sys import *`), rather than individual bindings (e.g., individual members in `sys`).
|
||||||
star_imports: Vec<StarImportation<'a>>,
|
star_imports: Vec<StarImport<'a>>,
|
||||||
|
|
||||||
/// A map from bound name to binding ID.
|
/// A map from bound name to binding ID.
|
||||||
bindings: FxHashMap<&'a str, BindingId>,
|
bindings: FxHashMap<&'a str, BindingId>,
|
||||||
|
|
@ -126,7 +126,7 @@ impl<'a> Scope<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
|
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
|
||||||
pub fn add_star_import(&mut self, import: StarImportation<'a>) {
|
pub fn add_star_import(&mut self, import: StarImport<'a>) {
|
||||||
self.star_imports.push(import);
|
self.star_imports.push(import);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,7 +136,7 @@ impl<'a> Scope<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over all star imports (e.g., `from sys import *`) in this scope.
|
/// Returns an iterator over all star imports (e.g., `from sys import *`) in this scope.
|
||||||
pub fn star_imports(&self) -> impl Iterator<Item = &StarImportation<'a>> {
|
pub fn star_imports(&self) -> impl Iterator<Item = &StarImport<'a>> {
|
||||||
self.star_imports.iter()
|
self.star_imports.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue