Remove name field from import binding kinds (#4817)

This commit is contained in:
Charlie Marsh 2023-06-02 23:02:47 -04:00 committed by GitHub
parent fcfd6ad129
commit 26b1dd0ca2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 88 additions and 120 deletions

View file

@ -48,39 +48,34 @@ impl<'a> Binding<'a> {
/// Return `true` if this binding redefines the given binding.
pub fn redefines(&self, existing: &'a Binding) -> bool {
match &self.kind {
BindingKind::Importation(Importation { full_name, .. }) => {
BindingKind::Importation(Importation { full_name }) => {
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
full_name: existing,
..
}) = &existing.kind
{
return full_name == existing;
}
}
BindingKind::FromImportation(FromImportation { full_name, .. }) => {
BindingKind::FromImportation(FromImportation { full_name }) => {
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
full_name: existing,
..
}) = &existing.kind
{
return full_name == existing;
}
}
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name, .. }) => {
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => {
match &existing.kind {
BindingKind::Importation(Importation {
full_name: existing,
..
})
| BindingKind::SubmoduleImportation(SubmoduleImportation {
full_name: existing,
..
}) => {
return full_name == existing;
}
BindingKind::FromImportation(FromImportation {
full_name: existing,
..
}) => {
return full_name == existing;
}
@ -211,37 +206,34 @@ pub struct Export<'a> {
pub names: Vec<&'a str>,
}
/// A binding for an `import`, keyed on the name to which the import is bound.
/// Ex) `import foo` would be keyed on "foo".
/// Ex) `import foo as bar` would be keyed on "bar".
#[derive(Clone, Debug)]
pub struct Importation<'a> {
/// The name to which the import is bound.
/// Given `import foo`, `name` would be "foo".
/// Given `import foo as bar`, `name` would be "bar".
pub name: &'a str,
/// The full name of the module being imported.
/// Given `import foo`, `full_name` would be "foo".
/// Given `import foo as bar`, `full_name` would be "foo".
/// Ex) Given `import foo`, `full_name` would be "foo".
/// Ex) Given `import foo as bar`, `full_name` would be "foo".
pub full_name: &'a str,
}
/// A binding for a member imported from a module, keyed on the name to which the member is bound.
/// Ex) `from foo import bar` would be keyed on "bar".
/// Ex) `from foo import bar as baz` would be keyed on "baz".
#[derive(Clone, Debug)]
pub struct FromImportation<'a> {
/// The name to which the import is bound.
/// Given `from foo import bar`, `name` would be "bar".
/// Given `from foo import bar as baz`, `name` would be "baz".
pub name: &'a str,
/// The full name of the module being imported.
/// Given `from foo import bar`, `full_name` would be "foo.bar".
/// Given `from foo import bar as baz`, `full_name` would be "foo.bar".
pub struct FromImportation {
/// The full name of the member being imported.
/// Ex) Given `from foo import bar`, `full_name` would be "foo.bar".
/// Ex) Given `from foo import bar as baz`, `full_name` would be "foo.bar".
pub full_name: String,
}
/// 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".
#[derive(Clone, Debug)]
pub struct SubmoduleImportation<'a> {
/// The parent module imported by the submodule import.
/// Given `import foo.bar`, `module` would be "foo".
pub name: &'a str,
/// The full name of the submodule being imported.
/// Given `import foo.bar`, `full_name` would be "foo.bar".
/// Ex) Given `import foo.bar`, `full_name` would be "foo.bar".
pub full_name: &'a str,
}
@ -261,7 +253,7 @@ pub enum BindingKind<'a> {
Export(Export<'a>),
FutureImportation,
Importation(Importation<'a>),
FromImportation(FromImportation<'a>),
FromImportation(FromImportation),
SubmoduleImportation(SubmoduleImportation<'a>),
}

View file

@ -145,7 +145,9 @@ impl<'a> SemanticModel<'a> {
self.bindings[binding_id].references.push(reference_id);
// Mark any submodule aliases as used.
if let Some(binding_id) = self.resolve_submodule(ScopeId::global(), binding_id) {
if let Some(binding_id) =
self.resolve_submodule(symbol, ScopeId::global(), binding_id)
{
let reference_id = self.references.push(ScopeId::global(), range, context);
self.bindings[binding_id].references.push(reference_id);
}
@ -181,7 +183,7 @@ impl<'a> SemanticModel<'a> {
self.bindings[binding_id].references.push(reference_id);
// Mark any submodule aliases as used.
if let Some(binding_id) = self.resolve_submodule(scope_id, binding_id) {
if let Some(binding_id) = self.resolve_submodule(symbol, scope_id, binding_id) {
let reference_id = self.references.push(self.scope_id, range, context);
self.bindings[binding_id].references.push(reference_id);
}
@ -238,7 +240,12 @@ impl<'a> SemanticModel<'a> {
}
/// Given a `BindingId`, return the `BindingId` of the submodule import that it aliases.
fn resolve_submodule(&self, scope_id: ScopeId, binding_id: BindingId) -> Option<BindingId> {
fn resolve_submodule(
&self,
symbol: &str,
scope_id: ScopeId,
binding_id: BindingId,
) -> Option<BindingId> {
// If the name of a submodule import is the same as an alias of another import, and the
// alias is used, then the submodule import should be marked as used too.
//
@ -249,21 +256,17 @@ impl<'a> SemanticModel<'a> {
// import pyarrow.csv
// print(pa.csv.read_csv("test.csv"))
// ```
let (name, full_name) = match &self.bindings[binding_id].kind {
BindingKind::Importation(Importation { name, full_name }) => (*name, *full_name),
BindingKind::SubmoduleImportation(SubmoduleImportation { name, full_name }) => {
(*name, *full_name)
}
BindingKind::FromImportation(FromImportation { name, full_name }) => {
(*name, full_name.as_str())
}
let full_name = match &self.bindings[binding_id].kind {
BindingKind::Importation(Importation { full_name }) => *full_name,
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => *full_name,
BindingKind::FromImportation(FromImportation { full_name }) => full_name.as_str(),
_ => return None,
};
let has_alias = full_name
.split('.')
.last()
.map(|segment| segment != name)
.map(|segment| segment != symbol)
.unwrap_or_default();
if !has_alias {
return None;
@ -285,31 +288,18 @@ impl<'a> SemanticModel<'a> {
///
/// ...then `resolve_call_path(${python_version})` will resolve to `sys.version_info`.
pub fn resolve_call_path(&'a self, value: &'a Expr) -> Option<CallPath<'a>> {
let Some(call_path) = collect_call_path(value) else {
return None;
};
let Some(head) = call_path.first() else {
return None;
};
let Some(binding) = self.find_binding(head) else {
return None;
};
let call_path = collect_call_path(value)?;
let head = call_path.first()?;
let binding = self.find_binding(head)?;
match &binding.kind {
BindingKind::Importation(Importation {
full_name: name, ..
})
| BindingKind::SubmoduleImportation(SubmoduleImportation { name, .. }) => {
BindingKind::Importation(Importation { full_name: name }) => {
if name.starts_with('.') {
if let Some(module) = &self.module_path {
let mut source_path = from_relative_import(module, name);
if source_path.is_empty() {
None
} else {
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
} else {
let mut source_path = from_relative_import(self.module_path?, name);
if source_path.is_empty() {
None
} else {
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
} else {
let mut source_path: CallPath = from_unqualified_name(name);
@ -317,20 +307,20 @@ impl<'a> SemanticModel<'a> {
Some(source_path)
}
}
BindingKind::FromImportation(FromImportation {
full_name: name, ..
}) => {
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name: name }) => {
let name = name.split('.').next().unwrap_or(name);
let mut source_path: CallPath = from_unqualified_name(name);
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
BindingKind::FromImportation(FromImportation { full_name: name }) => {
if name.starts_with('.') {
if let Some(module) = &self.module_path {
let mut source_path = from_relative_import(module, name);
if source_path.is_empty() {
None
} else {
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
} else {
let mut source_path = from_relative_import(self.module_path?, name);
if source_path.is_empty() {
None
} else {
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
} else {
let mut source_path: CallPath = from_unqualified_name(name);
@ -366,13 +356,13 @@ impl<'a> SemanticModel<'a> {
member: &str,
) -> Option<ImportedName> {
self.scopes().enumerate().find_map(|(scope_index, scope)| {
scope.binding_ids().find_map(|binding_id| {
scope.bindings().find_map(|(name, binding_id)| {
let binding = &self.bindings[binding_id];
match &binding.kind {
// Ex) Given `module="sys"` and `object="exit"`:
// `import sys` -> `sys.exit`
// `import sys as sys2` -> `sys2.exit`
BindingKind::Importation(Importation { name, full_name }) => {
BindingKind::Importation(Importation { full_name }) => {
if full_name == &module {
if let Some(source) = binding.source {
// Verify that `sys` isn't bound in an inner scope.
@ -393,7 +383,7 @@ impl<'a> SemanticModel<'a> {
// Ex) Given `module="os.path"` and `object="join"`:
// `from os.path import join` -> `join`
// `from os.path import join as join2` -> `join2`
BindingKind::FromImportation(FromImportation { name, full_name }) => {
BindingKind::FromImportation(FromImportation { full_name }) => {
if let Some((target_module, target_member)) = full_name.split_once('.') {
if target_module == module && target_member == member {
if let Some(source) = binding.source {
@ -415,8 +405,8 @@ impl<'a> SemanticModel<'a> {
}
// Ex) Given `module="os"` and `object="name"`:
// `import os.path ` -> `os.name`
BindingKind::SubmoduleImportation(SubmoduleImportation { name, .. }) => {
if name == &module {
BindingKind::SubmoduleImportation(SubmoduleImportation { .. }) => {
if name == module {
if let Some(source) = binding.source {
// Verify that `os` isn't bound in an inner scope.
if self