Skip to content

Add CLI flag to override typeshed #488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pyrefly/lib/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ struct ConfigOverrideArgs {
/// after first checking `search_path` and `typeshed`.
#[arg(long, env = clap_env("SITE_PACKAGE_PATH"))]
site_package_path: Option<Vec<PathBuf>>,
/// Override the bundled typeshed with a custom path.
#[arg(long = "typeshed-path", env = clap_env("TYPESHED_PATH"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: arg(long, ...) does the same thing as arg(long = "typeshed-path", ...), but specifying "typeshed-path" is more prone to typos/less resilient to refactors. Can we just leave this as arg(long, ...)?

typeshed_path: Option<PathBuf>,
/// The Python executable that will be queried for `python_version`
/// `python_platform`, or `site_package_path` if any of the values are missing.
#[arg(long, env = clap_env("PYTHON_INTERPRETER"), value_name = "EXE_PATH")]
Expand Down Expand Up @@ -555,6 +558,9 @@ impl Args {
config.python_environment.site_package_path = Some(x.clone());
config.python_environment.site_package_path_source = SitePackagePathSource::CommandLine;
}
if let Some(x) = &self.config_override.typeshed_path {
config.typeshed_path = Some(x.clone());
}
if let Some(x) = &self.config_override.python_interpreter {
config.python_interpreter = Some(x.clone());
}
Expand Down
38 changes: 35 additions & 3 deletions pyrefly/lib/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ pub struct ConfigFile {
)]
pub fallback_search_path: Vec<PathBuf>,

/// Override the bundled typeshed with a custom path.
#[serde(skip)]
pub typeshed_path: Option<PathBuf>,

// TODO(connernilsen): make this mutually exclusive with venv/conda env
/// The python executable that will be queried for `python_version`,
/// `python_platform`, or `site_package_path` if any of the values are missing.
Expand Down Expand Up @@ -302,6 +306,7 @@ impl Default for ConfigFile {
custom_module_paths: Default::default(),
use_untyped_imports: true,
ignore_missing_source: true,
typeshed_path: None,
}
}
}
Expand Down Expand Up @@ -354,14 +359,38 @@ impl ConfigFile {
Err(FindError::Ignored)
} else if let Some(path) = find_module_in_search_path(module, self.search_path()) {
Ok(path)
} else if let Some(custom_typeshed_path) = &self.typeshed_path {
// Check custom typeshed stdlib path before bundled typeshed
let stdlib_path = custom_typeshed_path.join("stdlib");
if let Some(path) = find_module_in_search_path(module, std::iter::once(&stdlib_path)) {
Ok(path)
} else if let Some(path) = typeshed()
.map_err(|err| FindError::not_found(err, module))?
.find(module)
{
Ok(path)
} else if let Some(path) = find_module_in_search_path(module, self.fallback_search_path.iter()) {
Ok(path)
} else if let Some(path) = find_module_in_site_package_path(
module,
self.site_package_path(),
self.use_untyped_imports,
self.ignore_missing_source,
)? {
Ok(path)
} else {
Err(FindError::import_lookup_path(
self.structured_import_lookup_path(),
module,
&self.source,
))
}
Comment on lines +367 to +387
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, it looks like this part might have been duplicated from below. Can you remove this?

} else if let Some(path) = typeshed()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only use the builtin typeshed if one isn't provided. Can we add an extra condition for that here?

} else if self.typeshed_path.is_none() && let Some(path) = ...

.map_err(|err| FindError::not_found(err, module))?
.find(module)
{
Ok(path)
} else if let Some(path) =
find_module_in_search_path(module, self.fallback_search_path.iter())
{
} else if let Some(path) = find_module_in_search_path(module, self.fallback_search_path.iter()) {
Ok(path)
} else if let Some(path) = find_module_in_site_package_path(
module,
Expand Down Expand Up @@ -823,6 +852,7 @@ mod tests {
}],
use_untyped_imports: true,
ignore_missing_source: true,
typeshed_path: None,
}
);
}
Expand Down Expand Up @@ -1033,6 +1063,7 @@ mod tests {
}],
use_untyped_imports: false,
ignore_missing_source: false,
typeshed_path: None,
};

let path_str = with_sep("path/to/my/config");
Expand Down Expand Up @@ -1069,6 +1100,7 @@ mod tests {
}],
use_untyped_imports: false,
ignore_missing_source: false,
typeshed_path: None,
};
assert_eq!(config, expected_config);
}
Expand Down