Skip to content

Commit

Permalink
Fix invalid version error when using with custom toolchain (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
ischaojie authored May 28, 2023
1 parent 609cabd commit ca0a923
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
18 changes: 9 additions & 9 deletions rye/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use clap::{Parser, ValueEnum};
use console::style;
use license::License;
use minijinja::{context, Environment};
use pep440_rs::{Version, VersionSpecifier};
use pep440_rs::VersionSpecifier;
use serde::Serialize;

use crate::config::Config;
use crate::platform::{get_default_author, get_latest_cpython, get_python_version_from_pyenv_pin};
use crate::sources::PythonVersion;
use crate::utils::is_inside_git_work_tree;

#[derive(ValueEnum, Copy, Clone, Serialize, Debug)]
Expand Down Expand Up @@ -156,23 +157,22 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
.unwrap_or_else(|| cfg.default_requires_python()),
};
let py = match cmd.py {
Some(py) => py,
None => {
let version = get_python_version_from_pyenv_pin()
.map(Ok)
.unwrap_or_else(get_latest_cpython)?;
format!("{}.{}.{}", version.major, version.minor, version.patch)
Some(py) => {
PythonVersion::from_str(&py).map_err(|msg| anyhow!("invalid version: {}", msg))?
}
None => get_python_version_from_pyenv_pin()
.map(Ok)
.unwrap_or_else(get_latest_cpython)?,
};
if !VersionSpecifier::from_str(&requires_python)
.map_err(|msg| anyhow!("invalid version specifier: {}", msg))?
.contains(&Version::from_str(&py).map_err(|msg| anyhow!("invalid version: {}", msg))?)
.contains(&py.clone().into())
{
eprintln!(
"{} conflicted python version with project's requires-python, will auto fix it.",
style("warning:").red()
);
requires_python = format!(">= {}", py.split('.').take(2).collect::<Vec<_>>().join("."));
requires_python = format!(">= {}.{}", py.major, py.minor);
}

let name = slug::slugify(
Expand Down
17 changes: 17 additions & 0 deletions rye/src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ impl fmt::Display for PythonVersion {
}
}

impl From<PythonVersion> for Version {
fn from(value: PythonVersion) -> Self {
Version {
epoch: 0,
release: vec![
value.major as usize,
value.minor as usize,
value.patch as usize,
],
pre: None,
post: None,
dev: None,
local: None,
}
}
}

/// Internal descriptor for a python version request.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)]
pub struct PythonVersionRequest {
Expand Down

0 comments on commit ca0a923

Please sign in to comment.