Fill-in missing implementation for is_native_module_file_name (#5410)

## Summary

This was just an oversight -- the last remaining `todo!()` that I never
filled in. We clearly don't have any test coverage for it yet, but this
mimics the Pyright implementation.
This commit is contained in:
Charlie Marsh 2023-06-28 10:50:54 -04:00 committed by GitHub
parent 979049b2a6
commit ea7bb199bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 23 deletions

View file

@ -1,6 +1,7 @@
//! Resolves Python imports to their corresponding files on disk.
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use log::debug;
@ -138,18 +139,21 @@ fn resolve_module_descriptor(
} else {
if allow_native_lib && dir_path.is_dir() {
// We couldn't find a `.py[i]` file; search for a native library.
if let Some(native_lib_path) = dir_path
.read_dir()
.unwrap()
.flatten()
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
.find(|entry| {
native_module::is_native_module_file_name(&dir_path, &entry.path())
})
{
debug!("Resolved import with file: {native_lib_path:?}");
is_native_lib = true;
resolved_paths.push(native_lib_path.path());
if let Some(module_name) = dir_path.file_name().and_then(OsStr::to_str) {
if let Some(native_lib_path) = dir_path
.read_dir()
.unwrap()
.flatten()
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
.map(|entry| entry.path())
.find(|path| {
native_module::is_native_module_file_name(module_name, path)
})
{
debug!("Resolved import with file: {native_lib_path:?}");
is_native_lib = true;
resolved_paths.push(native_lib_path);
}
}
}
@ -427,8 +431,13 @@ fn is_namespace_package_resolved(
implicit_imports: &BTreeMap<String, ImplicitImport>,
) -> bool {
if !module_descriptor.imported_symbols.is_empty() {
// Pyright uses `!Array.from(moduleDescriptor.importedSymbols.keys()).some((symbol) => implicitImports.has(symbol))`.
// But that only checks if any of the symbols are in the implicit imports?
// TODO(charlie): Pyright uses:
//
// ```typescript
// !Array.from(moduleDescriptor.importedSymbols.keys()).some((symbol) => implicitImports.has(symbol))`
// ```
//
// However, that only checks if _any_ of the symbols are in the implicit imports.
for symbol in &module_descriptor.imported_symbols {
if !implicit_imports.contains_key(symbol) {
return false;