Skip to content

Commit

Permalink
Add major/minor/micro for Version
Browse files Browse the repository at this point in the history
  • Loading branch information
ischaojie committed May 5, 2023
1 parent 47d9691 commit ce1dba2
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 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
30 changes: 30 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ pub struct Version {
/// > identifier by a plus. Local version labels have no specific semantics assigned, but some
/// > syntactic restrictions are imposed.
pub local: Option<Vec<LocalSegment>>,
/// The first item of release or 0 if unavailable.
pub major: usize,
/// The second item of release or 0 if unavailable.
pub minor: usize,
/// The third item of release or 0 if unavailable.
pub micro: usize,
}

#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -344,6 +350,21 @@ 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 {
self.0.major
}
/// The second item of release or 0 if unavailable.
#[getter]
pub fn minor(&self) -> usize {
self.0.minor
}
/// The third item of release or 0 if unavailable.
#[getter]
pub fn micro(&self) -> usize {
self.0.micro
}

/// Parses a PEP 440 version string
#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -427,6 +448,9 @@ impl Version {
post: None,
dev: None,
local: None,
major: 0,
minor: 0,
micro: 0,
}
}

Expand Down Expand Up @@ -765,6 +789,9 @@ impl Version {
.split('.')
.map(|segment| segment.parse::<usize>().map_err(|err| err.to_string()))
.collect::<Result<Vec<usize>, String>>()?;
let major = if !release.is_empty() { release[0] } else { 0 };
let minor = if release.len() >= 2 { release[1] } else { 0 };
let micro = if release.len() >= 3 { release[2] } else { 0 };
let star = captures.name("trailing_dot_star").is_some();
if star {
if pre.is_some() {
Expand All @@ -790,6 +817,9 @@ impl Version {
post,
dev,
local,
major,
minor,
micro,
};
Ok((version, star))
}
Expand Down
20 changes: 16 additions & 4 deletions src/version_specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,10 @@ mod test {
pre: None,
post: None,
dev: None,
local: None
local: None,
major: 0,
minor: 9,
micro: 0,
}
},
VersionSpecifier {
Expand All @@ -1049,7 +1052,10 @@ mod test {
pre: None,
post: None,
dev: None,
local: None
local: None,
major: 1,
minor: 0,
micro: 0,
}
},
VersionSpecifier {
Expand All @@ -1060,7 +1066,10 @@ mod test {
pre: None,
post: None,
dev: None,
local: None
local: None,
major: 1,
minor: 3,
micro: 4,
}
},
VersionSpecifier {
Expand All @@ -1071,7 +1080,10 @@ mod test {
pre: None,
post: None,
dev: None,
local: None
local: None,
major: 2,
minor: 0,
micro: 0,
}
}
]
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 ce1dba2

Please sign in to comment.