Skip to content

Commit

Permalink
Merge pull request #9 from ischaojie/o
Browse files Browse the repository at this point in the history
Add major/minor/micro for Version
  • Loading branch information
konstin committed May 6, 2023
2 parents 47d9691 + 1a8d0ae commit a692fd7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.pytest_cache
/target
__pycache__
.venv
15 changes: 7 additions & 8 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ unicode-width = "0.1.10"

[dev-dependencies]
indoc = "2.0.1"

[package.metadata.maturin]
name = "pep440_rs._pep440_rs"
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build-backend = "maturin"
[tool.maturin]
features = ["pyo3"]
python-source = "python"
module-name = "pep440_rs._pep440_rs"

[tool.ruff.per-file-ignores]
"python/pep440_rs/__init__.py" = ["F403", "F405"]
"python/pep440_rs/__init__.py" = ["F403", "F405"]
5 changes: 5 additions & 0 deletions python/pep440_rs/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Generated by `stubgen -p pep440_rs`
from typing import Any, ClassVar


class Version:
dev: Any
epoch: Any
post: Any
pre: Any
release: Any
major: Any
minor: Any
micro: Any

@classmethod
def __init__(cls, *args, **kwargs) -> None: ...
Expand All @@ -20,6 +24,7 @@ class Version:
def __lt__(self, other) -> Any: ...
def __ne__(self, other) -> Any: ...


class VersionSpecifier:
__hash__: ClassVar[None] = ...

Expand Down
28 changes: 28 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,33 @@ impl PyVersion {
pub fn dev(&self) -> Option<usize> {
self.0.dev
}
/// The first item of release or 0 if unavailable.
#[getter]
pub fn major(&self) -> usize {
if !self.release().is_empty() {
self.release()[0]
} else {
0
}
}
/// The second item of release or 0 if unavailable.
#[getter]
pub fn minor(&self) -> usize {
if self.release().len() > 1 {
self.release()[1]
} else {
0
}
}
/// The third item of release or 0 if unavailable.
#[getter]
pub fn micro(&self) -> usize {
if self.release().len() > 2 {
self.release()[2]
} else {
0
}
}

/// Parses a PEP 440 version string
#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -765,6 +792,7 @@ impl Version {
.split('.')
.map(|segment| segment.parse::<usize>().map_err(|err| err.to_string()))
.collect::<Result<Vec<usize>, String>>()?;

let star = captures.name("trailing_dot_star").is_some();
if star {
if pre.is_some() {
Expand Down
3 changes: 3 additions & 0 deletions test/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def test_pep440():
assert not VersionSpecifier(">=1.1").contains(Version("1.1a1"))
assert Version("1.1") >= Version("1.1a1")
assert Version("2.0") in VersionSpecifier("==2")
assert Version("2.1").major == 2
assert Version("2.1").minor == 1
assert Version("2.1").micro == 0


def test_version_specifier():
Expand Down

0 comments on commit a692fd7

Please sign in to comment.