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:
Charlie Marsh 2023-06-19 12:09:10 -04:00 committed by GitHub
parent e3c12764f8
commit 94abf7f088
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 61 additions and 68 deletions

View file

@ -82,33 +82,33 @@ 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 { qualified_name }) => {
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
BindingKind::Import(Import { qualified_name }) => {
if let BindingKind::SubmoduleImport(SubmoduleImport {
qualified_name: existing,
}) = &existing.kind
{
return qualified_name == existing;
}
}
BindingKind::FromImportation(FromImportation { qualified_name }) => {
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
BindingKind::FromImport(FromImport { qualified_name }) => {
if let BindingKind::SubmoduleImport(SubmoduleImport {
qualified_name: existing,
}) = &existing.kind
{
return qualified_name == existing;
}
}
BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }) => {
match &existing.kind {
BindingKind::Importation(Importation {
BindingKind::Import(Import {
qualified_name: existing,
})
| BindingKind::SubmoduleImportation(SubmoduleImportation {
| BindingKind::SubmoduleImport(SubmoduleImport {
qualified_name: existing,
}) => {
return qualified_name == existing;
}
BindingKind::FromImportation(FromImportation {
BindingKind::FromImport(FromImport {
qualified_name: existing,
}) => {
return qualified_name == existing;
@ -118,7 +118,7 @@ impl<'a> Binding<'a> {
}
BindingKind::Deletion
| BindingKind::Annotation
| BindingKind::FutureImportation
| BindingKind::FutureImport
| BindingKind::Builtin => {
return false;
}
@ -128,20 +128,18 @@ impl<'a> Binding<'a> {
existing.kind,
BindingKind::ClassDefinition
| BindingKind::FunctionDefinition
| BindingKind::Importation(..)
| BindingKind::FromImportation(..)
| BindingKind::SubmoduleImportation(..)
| BindingKind::Import(..)
| BindingKind::FromImport(..)
| BindingKind::SubmoduleImport(..)
)
}
/// Returns the fully-qualified symbol name, if this symbol was imported from another module.
pub fn qualified_name(&self) -> Option<&str> {
match &self.kind {
BindingKind::Importation(Importation { qualified_name }) => Some(qualified_name),
BindingKind::FromImportation(FromImportation { qualified_name }) => {
Some(qualified_name)
}
BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
BindingKind::Import(Import { qualified_name }) => Some(qualified_name),
BindingKind::FromImport(FromImport { qualified_name }) => Some(qualified_name),
BindingKind::SubmoduleImport(SubmoduleImport { qualified_name }) => {
Some(qualified_name)
}
_ => None,
@ -152,11 +150,11 @@ impl<'a> Binding<'a> {
/// symbol was imported from another module.
pub fn module_name(&self) -> Option<&str> {
match &self.kind {
BindingKind::Importation(Importation { qualified_name })
| BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
BindingKind::Import(Import { qualified_name })
| BindingKind::SubmoduleImport(SubmoduleImport { 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
.rsplit_once('.')
.map_or(qualified_name, |(module, _)| module),
@ -275,7 +273,7 @@ impl<'a> FromIterator<Binding<'a>> for Bindings<'a> {
}
#[derive(Debug, Clone)]
pub struct StarImportation<'a> {
pub struct StarImport<'a> {
/// The level of the import. `None` or `Some(0)` indicate an absolute import.
pub level: Option<u32>,
/// 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 as bar` would be keyed on "bar".
#[derive(Debug, Clone)]
pub struct Importation<'a> {
pub struct Import<'a> {
/// The full name of the module being imported.
/// Ex) Given `import foo`, `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 as baz` would be keyed on "baz".
#[derive(Debug, Clone)]
pub struct FromImportation {
pub struct FromImport {
/// 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 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.
/// Ex) `import foo.bar` would be keyed on "foo".
#[derive(Debug, Clone)]
pub struct SubmoduleImportation<'a> {
pub struct SubmoduleImport<'a> {
/// The full name of the submodule being imported.
/// Ex) Given `import foo.bar`, `qualified_name` would be "foo.bar".
pub qualified_name: &'a str,
@ -401,25 +399,25 @@ pub enum BindingKind<'a> {
/// ```python
/// from __future__ import annotations
/// ```
FutureImportation,
FutureImport,
/// A binding for a straight `import`, like `foo` in:
/// ```python
/// import foo
/// ```
Importation(Importation<'a>),
Import(Import<'a>),
/// A binding for a member imported from a module, like `bar` in:
/// ```python
/// from foo import bar
/// ```
FromImportation(FromImportation),
FromImport(FromImport),
/// A binding for a submodule imported from a module, like `bar` in:
/// ```python
/// import foo.bar
/// ```
SubmoduleImportation(SubmoduleImportation<'a>),
SubmoduleImport(SubmoduleImport<'a>),
/// A binding for a deletion, like `x` in:
/// ```python

View file

@ -13,8 +13,8 @@ use ruff_python_stdlib::path::is_python_stub_file;
use ruff_python_stdlib::typing::is_typing_extension;
use crate::binding::{
Binding, BindingFlags, BindingId, BindingKind, Bindings, Exceptions, FromImportation,
Importation, SubmoduleImportation,
Binding, BindingFlags, BindingId, BindingKind, Bindings, Exceptions, FromImport, Import,
SubmoduleImport,
};
use crate::context::ExecutionContext;
use crate::definition::{Definition, DefinitionId, Definitions, Member, Module};
@ -417,7 +417,7 @@ impl<'a> SemanticModel<'a> {
let head = call_path.first()?;
let binding = self.find_binding(head)?;
match &binding.kind {
BindingKind::Importation(Importation {
BindingKind::Import(Import {
qualified_name: name,
}) => {
if name.starts_with('.') {
@ -434,7 +434,7 @@ impl<'a> SemanticModel<'a> {
Some(source_path)
}
}
BindingKind::SubmoduleImportation(SubmoduleImportation {
BindingKind::SubmoduleImport(SubmoduleImport {
qualified_name: 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));
Some(source_path)
}
BindingKind::FromImportation(FromImportation {
BindingKind::FromImport(FromImport {
qualified_name: name,
}) => {
if name.starts_with('.') {
@ -493,7 +493,7 @@ impl<'a> SemanticModel<'a> {
// Ex) Given `module="sys"` and `object="exit"`:
// `import sys` -> `sys.exit`
// `import sys as sys2` -> `sys2.exit`
BindingKind::Importation(Importation { qualified_name }) => {
BindingKind::Import(Import { qualified_name }) => {
if qualified_name == &module {
if let Some(source) = binding.source {
// 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"`:
// `from os.path import join` -> `join`
// `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 target_module == module && target_member == member {
@ -537,7 +537,7 @@ impl<'a> SemanticModel<'a> {
}
// Ex) Given `module="os"` and `object="name"`:
// `import os.path ` -> `os.name`
BindingKind::SubmoduleImportation(SubmoduleImportation { .. }) => {
BindingKind::SubmoduleImport(SubmoduleImport { .. }) => {
if name == module {
if let Some(source) = binding.source {
// Verify that `os` isn't bound in an inner scope.

View file

@ -8,7 +8,7 @@ use rustpython_parser::ast;
use ruff_index::{newtype_index, Idx, IndexSlice, IndexVec};
use crate::binding::{BindingId, StarImportation};
use crate::binding::{BindingId, StarImport};
use crate::globals::GlobalsId;
#[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
/// `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.
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.
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);
}
@ -136,7 +136,7 @@ impl<'a> Scope<'a> {
}
/// 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()
}