Skip to content

Rename from to try_from #75

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

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
2 changes: 1 addition & 1 deletion crates/pet-conda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Locator for Conda {
fn supported_categories(&self) -> Vec<PythonEnvironmentCategory> {
vec![PythonEnvironmentCategory::Conda]
}
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
// Possible we do not have the prefix, but this exe is in the bin directory and its a conda env or root conda install.
let mut prefix = env.prefix.clone();
if prefix.is_none() {
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-conda/tests/ci_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn detect_conda_root_from_path() {
let conda = Conda::from(&env);

let python_env = PythonEnv::new(exe, Some(conda_dir.clone()), None);
let env = conda.from(&python_env).unwrap();
let env = conda.try_from(&python_env).unwrap();

assert_eq!(env.manager.is_some(), true);

Expand Down Expand Up @@ -182,7 +182,7 @@ fn detect_conda_env_from_path() {
let conda = Conda::from(&env);

let python_env = PythonEnv::new(exe.clone(), Some(prefix.clone()), None);
let env = conda.from(&python_env).unwrap();
let env = conda.try_from(&python_env).unwrap();

assert_eq!(env.manager.is_some(), true);

Expand Down
4 changes: 2 additions & 2 deletions crates/pet-conda/tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn find_conda_env_without_manager() {
let path = resolve_test_path(&["unix", "conda_env_without_manager", "env_python_3"]);

let env = locator
.from(&PythonEnv::new(
.try_from(&PythonEnv::new(
path.join("bin").join("python").into(),
Some(path.clone().into()),
None,
Expand Down Expand Up @@ -70,7 +70,7 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() {
fs::write(history_file, history_contents).unwrap();

let env = locator
.from(&PythonEnv::new(
.try_from(&PythonEnv::new(
path.join("bin").join("python").into(),
Some(path.clone().into()),
None,
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait Locator: Send + Sync {
///
/// Note: The returned environment could have some missing information.
/// This is because the `from` will do a best effort to get the environment information without spawning Python.
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment>;
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment>;
/// Finds all environments specific to this locator.
fn find(&self, reporter: &dyn Reporter);
}
2 changes: 1 addition & 1 deletion crates/pet-homebrew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Locator for Homebrew {
fn supported_categories(&self) -> Vec<PythonEnvironmentCategory> {
vec![PythonEnvironmentCategory::Homebrew]
}
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
from(env)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/pet-linux-global-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Locator for LinuxGlobalPython {
vec![PythonEnvironmentCategory::LinuxGlobal]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if std::env::consts::OS == "macos" || std::env::consts::OS == "windows" {
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-mac-commandlinetools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Locator for MacCmdLineTools {
vec![PythonEnvironmentCategory::MacCommandLineTools]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if std::env::consts::OS != "macos" {
return None;
}
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Locator for MacCmdLineTools {
}
}
env.symlinks = Some(symlinks);
if let Some(env) = self.from(&env) {
if let Some(env) = self.try_from(&env) {
_reporter.report_environment(&env);
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/pet-mac-python-org/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Locator for MacPythonOrg {
vec![PythonEnvironmentCategory::MacPythonOrg]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if std::env::consts::OS != "macos" {
return None;
}
Expand Down Expand Up @@ -158,7 +158,8 @@ impl Locator for MacPythonOrg {
let executable = prefix.join("bin").join("python3");
let version = version::from_header_files(&prefix);

if let Some(env) = self.from(&PythonEnv::new(executable, Some(prefix), version)) {
if let Some(env) = self.try_from(&PythonEnv::new(executable, Some(prefix), version))
{
reporter.report_environment(&env);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-mac-xcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Locator for MacXCode {
vec![PythonEnvironmentCategory::MacCommandLineTools]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if std::env::consts::OS != "macos" {
return None;
}
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Locator for MacXCode {
// }
// }
// env.symlinks = Some(symlinks);
// if let Some(env) = self.from(&env) {
// if let Some(env) = self.try_from(&env) {
// _reporter.report_environment(&env);
// }
// }
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-pipenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Locator for PipEnv {
vec![PythonEnvironmentCategory::Pipenv]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if !is_pipenv(env, &self.env_vars) {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-poetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Locator for Poetry {
vec![PythonEnvironmentCategory::Poetry]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if !is_virtualenv(env) {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-pyenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Locator for PyEnv {
]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if let Some(prefix) = &env.prefix {
if is_conda_env(prefix) {
return None;
Expand Down
8 changes: 4 additions & 4 deletions crates/pet-pyenv/tests/pyenv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn resolve_pyenv_environment() {
};

// Resolve regular Python installs in Pyenv
let result = locator.from(&PythonEnv::new(
let result = locator.try_from(&PythonEnv::new(
resolve_test_path(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9/bin/python"]),
Some(resolve_test_path(&[
home.to_str().unwrap(),
Expand All @@ -448,7 +448,7 @@ fn resolve_pyenv_environment() {
assert_eq!(result.unwrap(), expected_3_9_9);

// Resolve regular virtual-envs in Pyenv
let result = locator.from(&PythonEnv::new(
let result = locator.try_from(&PythonEnv::new(
resolve_test_path(&[
home.to_str().unwrap(),
".pyenv/versions/my-virtual-env/bin/python",
Expand All @@ -463,7 +463,7 @@ fn resolve_pyenv_environment() {
assert_eq!(result.unwrap(), expected_virtual_env);

// Should not resolve conda envs in pyenv
let result = locator.from(&PythonEnv::new(
let result = locator.try_from(&PythonEnv::new(
resolve_test_path(&[
home.to_str().unwrap(),
".pyenv/versions/anaconda-4.0.0/bin/python",
Expand All @@ -478,7 +478,7 @@ fn resolve_pyenv_environment() {
assert_eq!(result.is_none(), true);

// Should not resolve conda envs using Conda Locator
let result = conda.from(&PythonEnv::new(
let result = conda.try_from(&PythonEnv::new(
resolve_test_path(&[
home.to_str().unwrap(),
".pyenv/versions/anaconda-4.0.0/bin/python",
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-venv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Locator for Venv {
vec![PythonEnvironmentCategory::Venv]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if is_venv(env) {
let mut prefix = env.prefix.clone();
if prefix.is_none() {
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-virtualenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Locator for VirtualEnv {
vec![PythonEnvironmentCategory::VirtualEnv]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if is_virtualenv(env) {
let version = match env.version {
Some(ref v) => Some(v.clone()),
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-virtualenvwrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Locator for VirtualEnvWrapper {
vec![PythonEnvironmentCategory::VirtualEnvWrapper]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if !is_virtualenvwrapper(env, &self.env_vars) {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-windows-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Locator for WindowsRegistry {
vec![PythonEnvironmentCategory::WindowsRegistry]
}

fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
// Assume we create a virtual env from a python install,
// Then the exe in the virtual env bin will be a symlink to the homebrew python install.
// Hence the first part of the condition will be true, but the second part will be false.
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-windows-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Locator for WindowsStore {
}

#[cfg(windows)]
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
use pet_virtualenv::is_virtualenv;

// Assume we create a virtual env from a python install,
Expand All @@ -91,7 +91,7 @@ impl Locator for WindowsStore {
}

#[cfg(unix)]
fn from(&self, _env: &PythonEnv) -> Option<PythonEnvironment> {
fn try_from(&self, _env: &PythonEnv) -> Option<PythonEnvironment> {
None
}

Expand Down
16 changes: 9 additions & 7 deletions crates/pet/src/locators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ pub fn identify_python_environment_using_locators(
fallback_category: Option<PythonEnvironmentCategory>,
) -> Option<PythonEnvironment> {
let executable = env.executable.clone();
if let Some(env) = locators
.iter()
.fold(None, |e, loc| if e.is_some() { e } else { loc.from(env) })
{
if let Some(env) = locators.iter().fold(
None,
|e, loc| if e.is_some() { e } else { loc.try_from(env) },
) {
return Some(env);
}

Expand All @@ -98,9 +98,11 @@ pub fn identify_python_environment_using_locators(
// We try to get the interpreter info, hoping that the real exe returned might be identifiable.
if let Some(resolved_env) = ResolvedPythonEnv::from(&executable) {
let env = resolved_env.to_python_env();
if let Some(env) = locators
.iter()
.fold(None, |e, loc| if e.is_some() { e } else { loc.from(&env) })
if let Some(env) =
locators.iter().fold(
None,
|e, loc| if e.is_some() { e } else { loc.try_from(&env) },
)
{
trace!(
"Unknown Env ({:?}) in Path resolved as {:?}",
Expand Down
6 changes: 3 additions & 3 deletions crates/pet/tests/ci_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod common;
/// by spawning the Python executable
/// Verification 2:
/// For each enviornment, given the executable verify we can get the exact same information
/// Using the `locators.from` method (without having to find all environments).
/// Using the `locator.try_from` method (without having to find all environments).
/// I.e. we should be able to get the same information using only the executable.
/// Verification 3:
/// Similarly for each environment use one of the known symlinks and verify we can get the same information.
Expand Down Expand Up @@ -96,7 +96,7 @@ fn verify_validity_of_discovered_envs() {
}));
// Verification 2:
// For each enviornment, given the executable verify we can get the exact same information
// Using the `locators.from` method (without having to find all environments).
// Using the `locator.try_from` method (without having to find all environments).
// I.e. we should be able to get the same information using only the executable.
//
// Verification 3:
Expand Down Expand Up @@ -295,7 +295,7 @@ fn verify_we_can_get_same_env_inf_using_from_with_exe(
) {
let mut environment = environment.clone();

// Assume we were given a path to the exe, then we use the `locator.from` method.
// Assume we were given a path to the exe, then we use the `locator.try_from` method.
// We should be able to get the exct same information back given only the exe.
//
// Note: We will not not use the old locator objects, as we do not want any cached information.
Expand Down
Loading