Skip to content

Commit

Permalink
Implement FromPyObject for Version
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Sep 28, 2023
1 parent 6e322b5 commit 74c297a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pep440_rs"
version = "0.3.11"
version = "0.3.12"
description = "A library for python version numbers and specifiers, implementing PEP 440"
edition = "2021"
include = ["/src", "Changelog.md", "License-Apache", "License-BSD", "Readme.md", "pyproject.toml"]
Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.12

* Implement `FromPyObject` for `Version`

## 0.3.11

* CI fix
Expand Down
19 changes: 17 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use lazy_static::lazy_static;
#[cfg(feature = "pyo3")]
use pyo3::{
basic::CompareOp, exceptions::PyValueError, pyclass, pymethods, IntoPy, PyObject, PyResult,
Python,
basic::CompareOp, exceptions::PyValueError, pyclass, pymethods, FromPyObject, IntoPy, PyAny,
PyObject, PyResult, Python,
};
use regex::Captures;
use regex::Regex;
Expand Down Expand Up @@ -304,6 +304,13 @@ impl IntoPy<PyObject> for Version {
}
}

#[cfg(feature = "pyo3")]
impl<'source> FromPyObject<'source> for Version {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
Ok(ob.extract::<PyVersion>()?.0)
}
}

/// Workaround for https://github.com/PyO3/pyo3/pull/2786
#[cfg(feature = "pyo3")]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -819,6 +826,8 @@ impl Version {

#[cfg(test)]
mod test {
#[cfg(feature = "pyo3")]
use pyo3::pyfunction;
use std::str::FromStr;

use crate::{Version, VersionSpecifier};
Expand Down Expand Up @@ -1169,4 +1178,10 @@ mod test {
"You can't have both a trailing `.*` and a local version"
);
}

#[cfg(feature = "pyo3")]
#[pyfunction]
fn _convert_in_and_out(version: Version) -> Version {
version
}
}

0 comments on commit 74c297a

Please sign in to comment.