WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a675e0a

Browse files
John Van Schultzmeta-codesync[bot]
authored andcommitted
Update find_third_party_typeshed_stub to also find other third party stubs.
Summary: Currently we have the function find_third_party_typeshed_stub which looks to see if we have bundled typeshed stubs for a particular module. Now we have also expanded this to look for non-typeshed stubs as well. We do make a decision here to prioritize typeshed stubs over other bundled stubs. Since typeshed for now is updated more regularly, if we do have a stub from typeshed it might make more sense to use that one. If we do find a typeshed stub for a particular module, we will immediately return it rather than also checking for a non-typeshed third party stub. Reviewed By: kinto0 Differential Revision: D88486679 fbshipit-source-id: f39d051b42efbb97b18c67441effa535f64b5994
1 parent c0be794 commit a675e0a

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

pyrefly/lib/module/finder.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use vec1::Vec1;
1919

2020
use crate::config::config::ConfigFile;
2121
use crate::module::bundled::BundledStub;
22+
use crate::module::third_party::get_bundled_third_party;
2223
use crate::module::typeshed::typeshed;
2324
use crate::module::typeshed_third_party::typeshed_third_party;
2425
use crate::state::loader::FindError;
@@ -470,17 +471,34 @@ fn find_module_prefixes<'a>(
470471
results.iter().map(|(_, name)| *name).collect::<Vec<_>>()
471472
}
472473

473-
fn find_third_party_typeshed_stub(
474+
/// This function will find either third party typeshed stubs or other third party stubs
475+
/// Here a decision is being made to prioritize typeshed stubs over other third party stubs that are bundled.
476+
/// Since we run the typeshed update script with a more regular cadence, it is more likely that
477+
/// these stubs will be more up to date.
478+
fn find_third_party_stub(
474479
module: ModuleName,
475480
style_filter: Option<ModuleStyle>,
476481
) -> Option<FindingOrError<ModulePath>> {
477-
if matches!(style_filter, Some(ModuleStyle::Interface) | None) {
482+
let third_party_typeshed_stub = if matches!(style_filter, Some(ModuleStyle::Interface) | None) {
478483
typeshed_third_party().map_or_else(
479484
|err| Some(FindingOrError::Error(FindError::not_found(err, module))),
480485
|ts| ts.find(module).map(FindingOrError::new_finding),
481486
)
482487
} else {
483488
None
489+
};
490+
491+
if let Some(stub) = third_party_typeshed_stub {
492+
return Some(stub);
493+
}
494+
495+
if matches!(style_filter, Some(ModuleStyle::Interface) | None) {
496+
get_bundled_third_party().map_or_else(
497+
|err| Some(FindingOrError::Error(FindError::not_found(err, module))),
498+
|ts| ts.find(module).map(FindingOrError::new_finding),
499+
)
500+
} else {
501+
None
484502
}
485503
}
486504

@@ -515,7 +533,7 @@ pub fn find_import_filtered(
515533
let mut namespaces_found = vec![];
516534
let origin = origin.map(|p| p.as_path());
517535
let from_real_config_file = config.from_real_config_file();
518-
let typeshed_third_party_result = find_third_party_typeshed_stub(module, style_filter);
536+
let typeshed_third_party_result = find_third_party_stub(module, style_filter);
519537

520538
let typeshed_third_party_stub = match from_real_config_file {
521539
true => None,
@@ -2139,4 +2157,46 @@ mod tests {
21392157
panic!("Expected to find typeshed stub, got: {:?}", result);
21402158
}
21412159
}
2160+
2161+
#[test]
2162+
fn test_find_third_party_stub_prioritizes_typeshed_over_bundled() {
2163+
// 'requests' exists in typeshed third party stubs, so it should
2164+
// return BundledTypeshedThirdParty (typeshed is prioritized over other bundled stubs)
2165+
let result = find_third_party_stub(ModuleName::from_str("requests"), None);
2166+
assert!(result.is_some(), "Should find 'requests' stub");
2167+
2168+
if let Some(FindingOrError::Finding(finding)) = result {
2169+
assert!(
2170+
matches!(
2171+
finding.finding.details(),
2172+
ModulePathDetails::BundledTypeshedThirdParty(_)
2173+
),
2174+
"Expected BundledTypeshedThirdParty for 'requests', got: {:?}",
2175+
finding.finding.details()
2176+
);
2177+
} else {
2178+
panic!("Expected Finding result for 'requests', got: {:?}", result);
2179+
}
2180+
}
2181+
2182+
#[test]
2183+
fn test_find_third_party_stub_returns_bundled_when_not_in_typeshed() {
2184+
// 'conans' exists only in BundledThirdParty (from third_party/stubs/conans-stubs),
2185+
// not in typeshed, so it should return BundledThirdParty
2186+
let result = find_third_party_stub(ModuleName::from_str("conans"), None);
2187+
assert!(result.is_some(), "Should find 'conans' stub");
2188+
2189+
if let Some(FindingOrError::Finding(finding)) = result {
2190+
assert!(
2191+
matches!(
2192+
finding.finding.details(),
2193+
ModulePathDetails::BundledThirdParty(_)
2194+
),
2195+
"Expected BundledThirdParty for 'conans', got: {:?}",
2196+
finding.finding.details()
2197+
);
2198+
} else {
2199+
panic!("Expected Finding result for 'conans', got: {:?}", result);
2200+
}
2201+
}
21422202
}

0 commit comments

Comments
 (0)