Skip to content

Commit 592ab65

Browse files
authored
Remove unknown and make kind optional when kind is unknown (#92)
1 parent 7a1f006 commit 592ab65

26 files changed

Lines changed: 94 additions & 113 deletions

File tree

crates/pet-conda/src/environments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl CondaEnvironment {
5757
}
5858
}
5959
// This is a root env.
60-
let builder = PythonEnvironmentBuilder::new(PythonEnvironmentKind::Conda)
60+
let builder = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Conda))
6161
.executable(self.executable.clone())
6262
.version(self.version.clone())
6363
.prefix(Some(self.prefix.clone()))

crates/pet-conda/tests/ci_test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn detect_conda_root() {
5454
.unwrap();
5555
assert_eq!(env.prefix, conda_dir.clone().into());
5656
assert_eq!(env.name, Some("base".into()));
57-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
57+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
5858
assert_eq!(env.executable, Some(conda_dir.join("bin").join("python")));
5959
assert_eq!(env.version, Some(get_version(&info.python_version)));
6060

@@ -93,7 +93,7 @@ fn detect_conda_root_from_path() {
9393

9494
assert_eq!(env.prefix, conda_dir.clone().into());
9595
assert_eq!(env.name, Some("base".into()));
96-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
96+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
9797
assert_eq!(env.executable, Some(conda_dir.join("bin").join("python")));
9898
assert_eq!(env.version, Some(get_version(&info.python_version)));
9999
}
@@ -144,7 +144,7 @@ fn detect_new_conda_env() {
144144
let prefix = conda_dir.clone().join("envs").join(env_name);
145145
assert_eq!(env.prefix, prefix.clone().into());
146146
assert_eq!(env.name, Some(env_name.into()));
147-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
147+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
148148
assert_eq!(env.executable, prefix.join("bin").join("python").into());
149149
assert!(
150150
env.version.clone().unwrap_or_default().starts_with("3.10"),
@@ -193,7 +193,7 @@ fn detect_conda_env_from_path() {
193193

194194
assert_eq!(env.prefix, prefix.clone().into());
195195
assert_eq!(env.name, Some(env_name.into()));
196-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
196+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
197197
assert_eq!(env.executable, exe.clone().into());
198198
assert!(
199199
env.version.clone().unwrap_or_default().starts_with("3.10"),
@@ -245,7 +245,7 @@ fn detect_new_conda_env_without_python() {
245245
let prefix = conda_dir.clone().join("envs").join(env_name);
246246
assert_eq!(env.prefix, prefix.clone().into());
247247
assert_eq!(env.name, Some(env_name.into()));
248-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
248+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
249249
assert_eq!(env.executable.is_none(), true);
250250
assert_eq!(env.version.is_none(), true);
251251

@@ -293,7 +293,7 @@ fn detect_new_conda_env_created_with_p_flag_without_python() {
293293

294294
assert_eq!(env.prefix, prefix.clone().into());
295295
assert_eq!(env.name, None);
296-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
296+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
297297
assert_eq!(env.executable.is_none(), true);
298298
assert_eq!(env.version.is_none(), true);
299299

@@ -345,7 +345,7 @@ fn detect_new_conda_env_created_with_p_flag_with_python() {
345345

346346
assert_eq!(env.prefix, prefix.clone().into());
347347
assert_eq!(env.name, None);
348-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
348+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
349349
assert_eq!(env.executable, exe.into());
350350
assert!(
351351
env.version.clone().unwrap_or_default().starts_with("3.10"),

crates/pet-conda/tests/lib_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn find_conda_env_without_manager() {
2626

2727
assert_eq!(env.prefix, path.clone().into());
2828
assert_eq!(env.arch, Architecture::X64.into());
29-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
29+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
3030
assert_eq!(env.executable, path.join("bin").join("python").into());
3131
assert_eq!(env.version, "3.12.2".to_string().into());
3232
assert_eq!(env.manager, None);
@@ -75,7 +75,7 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() {
7575

7676
assert_eq!(env.prefix, path.clone().into());
7777
assert_eq!(env.arch, Architecture::X64.into());
78-
assert_eq!(env.kind, PythonEnvironmentKind::Conda);
78+
assert_eq!(env.kind, Some(PythonEnvironmentKind::Conda));
7979
assert_eq!(env.executable, path.join("bin").join("python").into());
8080
assert_eq!(env.version, "3.12.2".to_string().into());
8181
assert_eq!(

crates/pet-core/src/python_environment.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl PartialOrd for PythonEnvironmentKind {
4141

4242
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
4343
#[serde(rename_all = "camelCase")]
44-
#[derive(Debug)]
44+
#[derive(Debug, Default)]
4545
// Python environment.
4646
// Any item that has information is known to be accurate, if an item is missing it is unknown.
4747
pub struct PythonEnvironment {
@@ -51,7 +51,7 @@ pub struct PythonEnvironment {
5151
pub name: Option<String>,
5252
// Python executable, can be empty in the case of conda envs that do not have Python installed in them.
5353
pub executable: Option<PathBuf>,
54-
pub kind: PythonEnvironmentKind,
54+
pub kind: Option<PythonEnvironmentKind>,
5555
pub version: Option<String>,
5656
// SysPrefix for the environment.
5757
pub prefix: Option<PathBuf>,
@@ -93,31 +93,10 @@ impl PartialOrd for PythonEnvironment {
9393
}
9494
}
9595

96-
impl Default for PythonEnvironment {
97-
fn default() -> Self {
98-
Self {
99-
display_name: None,
100-
name: None,
101-
executable: None,
102-
// Sometimes we might not know the env type.
103-
// Lets never default these to System/Global or others as thats not true.
104-
// Not knowing does not mean it is a system env.
105-
kind: PythonEnvironmentKind::Unknown,
106-
version: None,
107-
prefix: None,
108-
manager: None,
109-
project: None,
110-
arch: None,
111-
symlinks: None,
112-
search_path: None,
113-
}
114-
}
115-
}
116-
11796
impl PythonEnvironment {
11897
pub fn new(
11998
executable: Option<PathBuf>,
120-
kind: PythonEnvironmentKind,
99+
kind: Option<PythonEnvironmentKind>,
121100
prefix: Option<PathBuf>,
122101
manager: Option<EnvManager>,
123102
version: Option<String>,
@@ -205,7 +184,7 @@ pub struct PythonEnvironmentBuilder {
205184
display_name: Option<String>,
206185
name: Option<String>,
207186
executable: Option<PathBuf>,
208-
kind: PythonEnvironmentKind,
187+
kind: Option<PythonEnvironmentKind>,
209188
version: Option<String>,
210189
prefix: Option<PathBuf>,
211190
manager: Option<EnvManager>,
@@ -216,7 +195,7 @@ pub struct PythonEnvironmentBuilder {
216195
}
217196

218197
impl PythonEnvironmentBuilder {
219-
pub fn new(kind: PythonEnvironmentKind) -> Self {
198+
pub fn new(kind: Option<PythonEnvironmentKind>) -> Self {
220199
Self {
221200
kind,
222201
display_name: None,
@@ -359,11 +338,11 @@ impl PythonEnvironmentBuilder {
359338
// Given a list of executables, return the one with the shortest path.
360339
// The shortest path is the most likely to be most user friendly.
361340
fn get_shortest_executable(
362-
kind: &PythonEnvironmentKind,
341+
kind: &Option<PythonEnvironmentKind>,
363342
exes: &Option<Vec<PathBuf>>,
364343
) -> Option<PathBuf> {
365344
// For windows store, the exe should always be the one in the WindowsApps folder.
366-
if *kind == PythonEnvironmentKind::WindowsStore {
345+
if *kind == Some(PythonEnvironmentKind::WindowsStore) {
367346
if let Some(exes) = exes {
368347
if let Some(exe) = exes.iter().find(|e| {
369348
e.to_string_lossy().contains("AppData")
@@ -398,7 +377,7 @@ pub fn get_environment_key(env: &PythonEnvironment) -> Option<PathBuf> {
398377
Some(exe.clone())
399378
} else if let Some(prefix) = &env.prefix {
400379
// If this is a conda env without Python, then the exe will be prefix/bin/python
401-
if env.kind == PythonEnvironmentKind::Conda {
380+
if env.kind == Some(PythonEnvironmentKind::Conda) {
402381
Some(prefix.join("bin").join(if cfg!(windows) {
403382
"python.exe"
404383
} else {

crates/pet-core/src/telemetry/inaccurate_python_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::python_environment::PythonEnvironmentKind;
88
/// And we will not report that as an inaccuracy.
99
pub struct InaccuratePythonEnvironmentInfo {
1010
/// Python Env kind
11-
pub kind: PythonEnvironmentKind,
11+
pub kind: Option<PythonEnvironmentKind>,
1212
/// Whether the actual exe is not what we expected.
1313
pub invalid_executable: Option<bool>,
1414
/// Whether the actual exe was not even in the list of symlinks that we expected.

crates/pet-homebrew/src/environments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn get_python_info(
4848
symlinks.sort();
4949
symlinks.dedup();
5050

51-
let env = PythonEnvironmentBuilder::new(PythonEnvironmentKind::Homebrew)
51+
let env = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Homebrew))
5252
.executable(Some(python_exe_from_bin_dir.to_path_buf()))
5353
.version(version)
5454
.prefix(get_prefix(resolved_exe))

crates/pet-linux-global-python/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn get_python_in_bin(env: &PythonEnv, is_64bit: bool) -> Option<PythonEnvironmen
209209
symlinks.dedup();
210210

211211
Some(
212-
PythonEnvironmentBuilder::new(PythonEnvironmentKind::LinuxGlobal)
212+
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::LinuxGlobal))
213213
.executable(Some(executable))
214214
.version(env.version.clone())
215215
.arch(if is_64bit {

crates/pet-mac-commandlinetools/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Locator for MacCmdLineTools {
171171
}
172172

173173
Some(
174-
PythonEnvironmentBuilder::new(PythonEnvironmentKind::MacCommandLineTools)
174+
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacCommandLineTools))
175175
.executable(Some(env.executable.clone()))
176176
.version(version)
177177
.prefix(prefix)

crates/pet-mac-python-org/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Locator for MacPythonOrg {
129129
symlinks.dedup();
130130

131131
Some(
132-
PythonEnvironmentBuilder::new(PythonEnvironmentKind::MacPythonOrg)
132+
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacPythonOrg))
133133
.executable(Some(executable.clone()))
134134
.version(Some(version))
135135
.prefix(Some(prefix.to_path_buf()))

crates/pet-mac-xcode/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl Locator for MacXCode {
158158
}
159159

160160
Some(
161-
PythonEnvironmentBuilder::new(PythonEnvironmentKind::MacXCode)
161+
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacXCode))
162162
.executable(Some(env.executable.clone()))
163163
.version(version)
164164
.prefix(prefix)

0 commit comments

Comments
 (0)