Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TripletVersion str() returns XYZ instead of X.Y.Z #17

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

v1.0.5 (2024-09-25)
-------------------

- str(TripletVersion) now returns XYZ instead of X.Y.Z to make serialization work inside AntaresWeb


v1.0.4 (2024-09-24)
-------------------

Expand Down
14 changes: 7 additions & 7 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Using the `antares-study-version` module is straightforward:
from antares.study.version import StudyVersion

version = StudyVersion(8, 7, 2) # patch component is not used
print(version) # 8.7
print(version) # 870
```

You can also create a version object from a dotted string:
Expand All @@ -24,7 +24,7 @@ You can also create a version object from a dotted string:
from antares.study.version import StudyVersion

version = StudyVersion.parse("8.7")
print(version) # 8.7
print(version) # 870
```

You can create a version object from a compact string:
Expand All @@ -33,7 +33,7 @@ You can create a version object from a compact string:
from antares.study.version import StudyVersion

version = StudyVersion.parse("870")
print(version) # 8.7
print(version) # 870
```

You can create a version object from an integer:
Expand All @@ -42,7 +42,7 @@ You can create a version object from an integer:
from antares.study.version import StudyVersion

version = StudyVersion.parse(870)
print(version) # 8.7
print(version) # 870
```

You can compare versions:
Expand All @@ -61,10 +61,10 @@ You can convert a version to string using format specifiers:
from antares.study.version import StudyVersion

version = StudyVersion(8, 7)
print(f"{version}") # 8.7
print(f"{version}d.d.d") # 8.7
print(f"{version:02d}") # 08.07
print(f"{version:03d}") # 08.07.00
print(f"{version:ddd}") # 870
print(version) # 870
```

You can convert a version to an integer:
Expand All @@ -84,7 +84,7 @@ Of course, the same operations can be done with `SolverVersion` objects, but wit
from antares.study.version import SolverVersion

version = SolverVersion(8, 7, 2)
print(version) # 8.7.2
print(version) # 872
```

Objects of the `StudyVersion` and `SolverVersion` classes can be compared to each other:
Expand Down
2 changes: 1 addition & 1 deletion src/antares/study/version/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Standard project metadata

__version__ = "1.0.4"
__version__ = "1.0.5"
__author__ = "RTE, Antares Web Team"
__date__ = "2024-07-06"
__credits__ = "© Réseau de Transport de l’Électricité (RTE)"
2 changes: 1 addition & 1 deletion src/antares/study/version/model/study_antares.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def to_ini_file(self, study_dir: t.Union[str, Path], update_save_date: bool = Tr

if self.version < DOTTED_VERSION: # type: ignore
# Old versions of Antares Studies used a different format for the version number
section_dict["version"] = f"{self.version:ddd}"
section_dict["version"] = f"{self.version:}"
else:
section_dict["version"] = f"{self.version.major}.{self.version.minor}"

Expand Down
20 changes: 10 additions & 10 deletions src/antares/study/version/model/study_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ def parse(cls: t.Type[T], other: object) -> T:
# Conversion methods

def __str__(self) -> str:
if self.patch:
return f"{self.major}.{self.minor}.{self.patch}"
elif self.minor:
return f"{self.major}.{self.minor}"
else:
return f"{self.major}"
return f"{int(self):03d}"

def __int__(self) -> int:
return self.major * 100 + self.minor * 10 + self.patch
Expand Down Expand Up @@ -95,12 +90,12 @@ def __format__(self, format_spec: str) -> str:
"""
Format the version number "X.Y.Z" according to the format specifier:

- "" => "X.Y.Z" or "X.Y" if patch is 0, or "X" if minor is 0.
- "" => "XYZ"
- "1d" => "X",
- "2d" => "X.Y",
- "01d" => "0X",
- "02d" => "0X.0Y"
- "ddd" => "XYZ"
- "d.d.d" => "X.Y.Z" or "X.Y" if patch is 0, or "X" if minor is 0.

:param format_spec: format specifier.

Expand All @@ -121,8 +116,13 @@ def __format__(self, format_spec: str) -> str:
return f"{major:02d}.{minor:02d}"
elif format_spec == "03d":
return f"{major:02d}.{minor:02d}.{patch:02d}"
elif format_spec == "ddd":
return f"{int(self):03d}"
elif format_spec == "d.d.d":
if self.patch:
return f"{self.major}.{self.minor}.{self.patch}"
elif self.minor:
return f"{self.major}.{self.minor}"
else:
return f"{self.major}"
else:
raise ValueError(f"Invalid format specifier: '{format_spec}'")

Expand Down
2 changes: 1 addition & 1 deletion tests/create_app/test_create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_no_template_available(self, tmp_path: Path):
study_dir = tmp_path.joinpath("my-new-study")
study_version = StudyVersion.parse("2.8")
app = CreateApp(study_dir=study_dir, caption="My New App", version=study_version, author="Robert Smith")
with pytest.raises(ApplicationError, match=re.escape(f"{study_version:2d}")):
with pytest.raises(ApplicationError, match=re.escape(str(study_version))):
app()

@pytest.mark.parametrize("study_version", AVAILABLE_VERSIONS)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_cli__create(self, tmp_path: Path, study_version: StudyVersion) -> None:
parser.read(study_antares_file, encoding="utf-8")
section_dict = dict(parser["antares"])
if study_version < 900:
expected_version = f"{study_version:ddd}"
expected_version = f"{study_version:}"
else:
expected_version = f"{study_version.major}.{study_version.minor}"

Expand Down
18 changes: 9 additions & 9 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test___str__(self) -> None:
version = SolverVersion(4, 5, 6)
result = version.__str__()
assert isinstance(result, str)
assert result == "4.5.6"
assert result == "456"

def test___int__(self) -> None:
version = SolverVersion(4, 5, 6)
Expand All @@ -51,14 +51,14 @@ def test_parse(self) -> None:
@pytest.mark.parametrize(
"format_spec, expected",
[
pytest.param("", "4.5.6", id="empty"),
pytest.param("d.d.d", "4.5.6", id="empty"),
pytest.param("1d", "4", id="format-1d"),
pytest.param("2d", "4.5", id="format-2d"),
pytest.param("3d", "4.5.6", id="format-3d"),
pytest.param("01d", "04", id="format-01d"),
pytest.param("02d", "04.05", id="format-02d"),
pytest.param("03d", "04.05.06", id="format-03d"),
pytest.param("ddd", "456", id="format-ddd"),
pytest.param("", "456", id="format-ddd"),
pytest.param("X", "", marks=pytest.mark.xfail(raises=ValueError, strict=True)),
],
)
Expand Down Expand Up @@ -93,7 +93,7 @@ def test___str__(self) -> None:
version = StudyVersion(4, 5)
result = version.__str__()
assert isinstance(result, str)
assert result == "4.5"
assert result == "450"

def test___int__(self) -> None:
version = StudyVersion(4, 5)
Expand All @@ -109,14 +109,14 @@ def test_parse(self) -> None:
@pytest.mark.parametrize(
"format_spec, expected",
[
pytest.param("", "4.5", id="empty"),
pytest.param("d.d.d", "4.5", id="empty"),
pytest.param("1d", "4", id="format-1d"),
pytest.param("2d", "4.5", id="format-2d"),
pytest.param("3d", "4.5.0", id="format-3d"),
pytest.param("01d", "04", id="format-01d"),
pytest.param("02d", "04.05", id="format-02d"),
pytest.param("03d", "04.05.00", id="format-03d"),
pytest.param("ddd", "450", id="format-ddd"),
pytest.param("", "450", id="format-ddd"),
pytest.param("X", "", marks=pytest.mark.xfail(raises=ValueError, strict=True)),
],
)
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_ini_file(self):

# We can write the study version to an INI file
version = StudyVersion(10, 2)
config["antares"]["version"] = str(version)
config["antares"]["version"] = version.__format__("d.d.d")

stream.seek(0)
config.write(stream)
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_version_str(self):
assert version == StudyVersion(8, 7)

# We can convert a version number to a string
assert str(version) == "8.7"
assert str(version) == "870"

# We can compare string versions using the standard comparison operators
assert version == "8.7"
Expand All @@ -272,7 +272,7 @@ def test_version_str(self):
# We can construct a version number from a string with a single number
version = StudyVersion.parse("8")
assert version == StudyVersion(8)
assert str(version) == "8"
assert str(version) == "800"

# noinspection PyDataclass,PyTypeChecker
def test_version_triplet(self):
Expand Down
Loading