Track ranges of names inside __all__ definitions (#10525)

This commit is contained in:
Alex Waygood 2024-03-22 18:38:40 +00:00 committed by GitHub
parent b74dd420fc
commit a06ffeb54e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 65 additions and 37 deletions

View file

@ -4,6 +4,7 @@ use std::ops::{Deref, DerefMut};
use bitflags::bitflags;
use ruff_index::{newtype_index, IndexSlice, IndexVec};
use ruff_python_ast::all::DunderAllName;
use ruff_python_ast::name::QualifiedName;
use ruff_python_ast::Stmt;
use ruff_source_file::Locator;
@ -370,7 +371,7 @@ impl<'a> FromIterator<Binding<'a>> for Bindings<'a> {
#[derive(Debug, Clone)]
pub struct Export<'a> {
/// The names of the bindings exported via `__all__`.
pub names: Box<[&'a str]>,
pub names: Box<[DunderAllName<'a>]>,
}
/// A binding for an `import`, keyed on the name to which the import is bound.

View file

@ -5,7 +5,7 @@ use std::fmt::Debug;
use std::ops::Deref;
use ruff_index::{newtype_index, IndexSlice, IndexVec};
use ruff_python_ast::{self as ast, Stmt};
use ruff_python_ast::{self as ast, all::DunderAllName, Stmt};
use ruff_text_size::{Ranged, TextRange};
use crate::analyze::visibility::{
@ -187,7 +187,7 @@ impl<'a> Definitions<'a> {
}
/// Resolve the visibility of each definition in the collection.
pub fn resolve(self, exports: Option<&[&str]>) -> ContextualizedDefinitions<'a> {
pub fn resolve(self, exports: Option<&[DunderAllName]>) -> ContextualizedDefinitions<'a> {
let mut definitions: IndexVec<DefinitionId, ContextualizedDefinition<'a>> =
IndexVec::with_capacity(self.len());
@ -201,7 +201,9 @@ impl<'a> Definitions<'a> {
MemberKind::Class(class) => {
let parent = &definitions[member.parent];
if parent.visibility.is_private()
|| exports.is_some_and(|exports| !exports.contains(&member.name()))
|| exports.is_some_and(|exports| {
!exports.iter().any(|export| export.name() == member.name())
})
{
Visibility::Private
} else {
@ -221,7 +223,9 @@ impl<'a> Definitions<'a> {
MemberKind::Function(function) => {
let parent = &definitions[member.parent];
if parent.visibility.is_private()
|| exports.is_some_and(|exports| !exports.contains(&member.name()))
|| exports.is_some_and(|exports| {
!exports.iter().any(|export| export.name() == member.name())
})
{
Visibility::Private
} else {